Search code examples
node.jszipadm-zip

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?


Solution

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