MayThrow
MayThrow

Reputation: 2201

Nodejs extract zip ansynchronously

I'm currently using adm-zip to extract a zip file to a certain path but its extractAllTo method is synchronous.

Is there a way I can extract zip files Asynchronously?

Upvotes: 2

Views: 5073

Answers (2)

peq42
peq42

Reputation: 392

well I know this question is old but since I found it on google(as one of the first results when searching "asynchronous extraction adm-zip"), and the answer given above doesn't use adm-zip but another lib, I thought I should answer it:

Adm-zip has a "extractAllToAsync" function(that sadly isn't present in their docs). Its basically the same as extractAllTo but has an extra optional parameter(a callback function) and works asynchronously(judging by the code, almost all of its work is asynchronous).

Usage:

var zip = new AdmZip(source); 
var zipEntries = zip.getEntries(); 
zip.extractAllToAsync(destination,overwrite? True/False,callback(error){})

Upvotes: 1

rdegges
rdegges

Reputation: 33824

Try using the async-unzip library on npm: https://www.npmjs.com/package/async-unzip

This works in-memory, and is 100% asynchronous, this will get you the behavior you want =)

Here's an example:

var async = require('async'),
        path = require('path'),
        ZipFile = require('async-unzip').ZipFile,
        zipFile = new ZipFile('/home/user/Name.app.dSYM.zip'),
        noMoreFiles = false;

async.whilst(function () {
    return !noMoreFiles;
}, function (cb) {
    async.waterfall([
        function (cb) {
            zipFile.getNextEntry(cb);
        },
        function (entry, cb) {
            if (entry) {
                if (entry.isFile) {
                    var match = entry.filename.match(/^[^\/]+\.dSYM\/Contents\/Resources\/DWARF\/(.+)/);

                    if (match) {
                        console.log(match);
                        console.log(entry);

                        async.waterfall([
                            function (cb) {
                                entry.getData(cb);
                            },
                            function (data, cb) {
                                // `data` is a binary data of the entry.
                                console.log(data.length);

                                cb();
                            }
                        ], cb);
                    }
                }
            } else {
                noMoreFiles = true;
            }

            cb();
        }
    ], cb);
}, function () {
    // DO NOT FORGET to call `close()` to release the open file descriptor,
    // otherwise, you will quickly run out of file descriptors.
    zipFile.close();
    console.log(arguments);
});

Upvotes: 1

Related Questions