【问题标题】:How to ZIP (compress) an SQLITE database file in Cordova?如何在 Cordova 中压缩(压缩)SQLITE 数据库文件?
【发布时间】:2019-04-06 12:55:40
【问题描述】:

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

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

支持的平台

Android iOS OS X Windows* 浏览器

  • 这些平台不支持 FileReader.readAsArrayBuffer 也不支持 FileWriter.write(blob)。

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

有什么想法吗?

【问题讨论】:

    标签: ios sqlite cordova zip windows-mobile


    【解决方案1】:

    我建议你再给 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));
            });
    

    “ZIP”功能:

        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");
        });
    } 
    

    【讨论】:

      【解决方案2】:

      为什么不使用 sqlite ".dump" 命令作为查询并通过 steam 获取结果然后压缩输出。即使文本转储会更大,当您压缩它时它也会得到合理的大小。我认为也有一些非常好的纯文本压缩算法,

      【讨论】:

        猜你喜欢
        • 2013-11-27
        • 2021-09-12
        • 1970-01-01
        • 2011-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多