Unzipping file in phonegap using zip.js

James Wong picture James Wong · Jan 24, 2014 · Viewed 12k times · Source

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://

Answer

James Wong picture James Wong · Oct 9, 2014

-- 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://