使用zip.js解压缩在phonegap中的文件 [英] Unzipping file in phonegap using zip.js

查看:182
本文介绍了使用zip.js解压缩在phonegap中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PhoneGap Cordova 3.3.0和最新版本的 zip.js 。脚本能够获取归档中的文件列表,但无法获取任何二进制数据。我使用 Hugeen 的修复,但没有运气。

 函数解压缩(fullsavepath,filename){
zip.useWebWorkers = false;
file_system.root.getFile(app_cache / temp.zip,{},function(fileEntry){
fileEntry.file(function(file){

zip.createReader (new zip.BlobReader(file),function(reader){

//从zip中获取所有条目
reader.getEntries(function(entries){
if .length){
//输出zip文件内容
for(var i = 0; i console.log(entries [i] .filename);
}

//尝试解压缩文件3
console.log('---- entries [2]'+ entries [2] .filename);
entries [2] .getData(new zip.BlobWriter(),function(unzipData){
console.log(unzipData);
},null,null);
}
});
},null);

})

},null);
}

这是我的控制台日志以及相关消息:

 控制台:---- entries [2] photoalbum / bpage1.jpg at file:///android_asset/www/dl/index.html:315 
控制台:processMessage失败:消息:S01 File821048650 AUEsDBBQAAAAIAEFVCEMFqmCrQAsHAMa3BwASAAAA at file:///android_asset/www/cordova.js:1034
控制台:processMessage失败:错误:TypeError:undefined不是文件中的函数: ///android_asset/www/cordova.js:1035
控制台:processMessage失败:Stack:TypeError:undefined不是函数
控制台:at inflate(file:/// android_asset / www / dl / js / vendor / zip / zip.js:429:18)
控制台:at file:///android_asset/www/dl/js/vendor/zip/zip.js:600:16
控制台:at BlobWriter.init(file:///android_asset/www/dl/js/vendor/zip/zip.js:297:4)
控制台:在文件:/// android_asset / www / dl / js /vendor/zip/zip.js:596:12
控制台:at [object Object]。< anonymous> (file:///android_asset/www/dl/js/vendor/zip/zip.js:203:5)
控制台:在file:///android_asset/www/plugins/org.apache.cordova。 core.file / www / FileReader.js:352:20
控制台:在Object.callbackFromNative(文件:///android_asset/www/cordova.js:292:54)
控制台:在processMessage file:///android_asset/www/cordova.js:1029:21)
控制台:在Function.processMessages(文件:///android_asset/www/cordova.js:1063:13)
控制台:at pollOnce(file:///android_asset/www/cordova.js:933:17)at file:///android_asset/www/cordova.js:1036

------ UPDATE 2014-04-07 -------------



最后,我们使用了另一种方法来解决问题。 Google为Android和iOS开发了一个原生插件 - https://github.com/MobileChromeApps/zip p>

这是相当简单的,而且老实说,我们使用的罕见插件之一成功做我们需要在第一次尝试。



示例:

  zip.unzip(cdvfile://localstorage/downloaded.zip,
cdvfile:// localstorage / my_data / path /,
function(){
console.log('Zip decompressed successful');
}
);

PS:Cordova已更新到3.4.0 - 我的原始问题使用了 file:/// android_asset / www 已全部替换为新的 cdvfile://

解决方案

- 从我的编辑上复原 -



解决问题。 Google为Android和iOS开发了一个原生插件 - https://github.com/MobileChromeApps/zip p>

这是相当简单的,而且老实说,我们使用的罕见插件之一成功做我们需要在第一次尝试。



示例:

  zip.unzip(cdvfile://localstorage/downloaded.zip,
cdvfile:// localstorage / my_data / path /,
function(){
console.log('Zip decompress successful');
}
);

PS:Cordova已更新到3.4.0 - 我的原始问题使用了 file:/// android_asset / www 已全部替换为新的 cdvfile://


am using PhoneGap Cordova 3.3.0 and latest version of zip.js. The script is able to get the list of files inside the archive but is unable to get any binary data. Am using the fix by Hugeen but no luck.

function decompress(fullsavepath, filename){
    zip.useWebWorkers = false;
    file_system.root.getFile("app_cache/temp.zip", {}, function(fileEntry){
        fileEntry.file(function (file) {

            zip.createReader(new zip.BlobReader(file), function(reader) {

                // get all entries from the zip
                reader.getEntries(function(entries) {
                    if (entries.length) {
                        //Output zip file contents
                        for(var i=0; i<entries.length;i++){
                            console.log(entries[i].filename);
                        }

                        //Try to decompress file no. 3
                        console.log('---- entries[2] '+entries[2].filename);
                        entries[2].getData(new zip.BlobWriter(), function(unzipData){
                            console.log(unzipData);
                        },null, null);
                    }
                });
            }, null);

        })

    },null);
}

Here is my console log with the relevant messages:

Console: ----entries[2] photoalbum/bpage1.jpg at file:///android_asset/www/dl/index.html:315
Console: processMessage failed: Message: S01 File821048650 AUEsDBBQAAAAIAEFVCEMFqmCrQAsHAMa3BwASAAAA at file:///android_asset/www/cordova.js:1034
Console: processMessage failed: Error: TypeError: undefined is not a function at file:///android_asset/www/cordova.js:1035
Console: processMessage failed: Stack: TypeError: undefined is not a function
Console:     at inflate (file:///android_asset/www/dl/js/vendor/zip/zip.js:429:18)
Console:     at file:///android_asset/www/dl/js/vendor/zip/zip.js:600:16
Console:     at BlobWriter.init (file:///android_asset/www/dl/js/vendor/zip/zip.js:297:4)
Console:     at file:///android_asset/www/dl/js/vendor/zip/zip.js:596:12
Console:     at [object Object].<anonymous> (file:///android_asset/www/dl/js/vendor/zip/zip.js:203:5)
Console:     at file:///android_asset/www/plugins/org.apache.cordova.core.file/www/FileReader.js:352:20
Console:     at Object.callbackFromNative (file:///android_asset/www/cordova.js:292:54)
Console:     at processMessage (file:///android_asset/www/cordova.js:1029:21)
Console:     at Function.processMessages (file:///android_asset/www/cordova.js:1063:13)
Console:     at pollOnce (file:///android_asset/www/cordova.js:933:17) at file:///android_asset/www/cordova.js:1036

------ UPDATE 2014-04-07 -------------

In the end we used an alternative approach to solve the problem. Google developed a native plugin for Android and iOS - https://github.com/MobileChromeApps/zip

It is quite straight forward, and honestly one of the rare plugins we used that succeeded doing what we needed on first try.

Example:

 zip.unzip("cdvfile://localstorage/downloaded.zip", 
           "cdvfile://localstorage/my_data/path/", 
           function(){
              console.log('Zip decompressed successfully');
           }
 );

PS: Cordova has since been updated to 3.4.0 - My original question used paths like file:///android_asset/www which has all been replaced with the new cdvfile://

解决方案

-- COPIED FROM MY EDIT ABOVE--

In the end we used an alternative approach to solve the problem. Google developed a native plugin for Android and iOS - https://github.com/MobileChromeApps/zip

It is quite straight forward, and honestly one of the rare plugins we used that succeeded doing what we needed on first try.

Example:

 zip.unzip("cdvfile://localstorage/downloaded.zip", 
           "cdvfile://localstorage/my_data/path/", 
           function(){
              console.log('Zip decompressed successfully');
           }
 );

PS: Cordova has since been updated to 3.4.0 - My original question used paths like file:///android_asset/www which has all been replaced with the new cdvfile://

这篇关于使用zip.js解压缩在phonegap中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆