gpt4 book ai didi

ios - 如何在 Cordova 中压缩(压缩)SQLITE 数据库文件?

转载 作者:IT王子 更新时间:2023-10-29 06:29:09 32 4
gpt4 key购买 nike

我当前在 Windows Mobile 和 iOS 上运行的 Cordova 应用程序需要“支持”模式。为此,我需要压缩一个 sqlite 数据库文件并将其上传到服务器。数据库必须压缩,因为它可能会增长超过 250MB,并且上传必须在没有 wifi 连接的情况下进行。

在网络上搜索了不同的方法,但所有方法都已过时或只能解决我的 iOS 或 Windows Mobile 问题。例如,在使用 Cordova 文件插件时,我在插件文档中遇到过:

Supported Platforms

Android iOS OS X Windows* Browser

  • These platforms do not support FileReader.readAsArrayBuffer nor FileWriter.write(blob).

这是我的方法:Cordova - Zip files and folders on iOS

有什么想法吗?

最佳答案

我建议您再给 FileReader() 一次机会。

在我的例子中可能与你的非常相似,我使用 FilerReader.readAsArrayBuffer 读取一个文件,然后使用 JSZip 库压缩它:http://stuartk.com/jszip

反对 cordova-file-plugin 的 API 文档 ( https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/ )“Windows*”->“这些平台不支持 FileReader.readAsArrayBuffer 或 FileWriter.write(blob))”我体验过 readAsArrayBuffer 在 Windows UWP 平台下工作,但速度较慢。

所以在我的例子中,有一个大约的文件。 50M 等了将近2分钟整个过程才结束!

尝试以下示例:

您需要适应您的路径,但这适用于 WINDOWS UWP 和 IOS(未使用 Android 对其进行测试,但这不是您的问题)。

此外,您还需要实现自己的错误处理程序 (errorHandler)。此解决方案使用 Promises,因为您必须等待文件被读取和压缩。

PS1:始终确保您的“设备准备就绪事件”已被触发,以便访问插件。

PS2:你可能会遇到没有访问数据库文件的权限,这可能与它正在被另一个进程使用有关。 确保数据库已关闭。 SQLITE:

        var sqlite = window.sqlitePlugin.openDatabase({ name: 'yourdb.db', location: 'default' });
sqlite.close(function () {
console.log("DONE closing db");
}, function (error) {
console.log("ERROR closing db");
console.log(JSON.stringify(error));
});

“压缩”功能:

    function zipFile(sourceFileName, targetFileName) {
return new Promise(function (resolve, reject) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
fs.root.getFile(sourceFileName, { create: false, exclusive: false }, function (fe) {
fe.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (data) {
var zip = new JSZip();
zip.file(sourceFileName, data.target.result);
zip.generateAsync({
type: "blob",
compression: "DEFLATE",
compressionOptions: {
level: 9
}
// level 9 means max. compression
// this may also take some time depending on the size of your file
// I tested it with a 50M file, it took about 65 sec.
}).then(

// following is post-zip in order to transfer the file to a server
function (blob) {
fs.root.getFile(targetFileName, { create: true, exclusive: false }, function (newzip) {
writeFile(newzip, blob, "application/zip").then(function () {
var f = blob;
var zipReader = new FileReader();

zipReader.onloadend = function (theFile) {
var base64 = window.btoa(theFile.target.result);
resolve(base64);
};
// need to "resolve" the zipped file as base64 in order to incluse it in my REST post (server-upload)
zipReader.readAsBinaryString(f);
});

});

}
)
};
reader.readAsArrayBuffer(file);
// this may take some time depending on the size of your file
// I tested it with a 50M file, it took about 72 sec.
}, errorHandler);
}, errorHandler);
});
});
}

前电话:

if (window.cordova) {
document.addEventListener('deviceready', function () {
zipFile("yourDatabaseFileName.db","compressedDatabaseFile.zip");
});
}

关于ios - 如何在 Cordova 中压缩(压缩)SQLITE 数据库文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53115869/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com