Nodejs异步提取zip

时间:2015-01-22 21:28:56

标签: node.js zip adm-zip

我目前正在使用adm-zip将zip文件解压缩到某个路径,但其extractAllTo方法是同步的。

有没有办法可以异步提取zip文件?

2 个答案:

答案 0 :(得分:1)

尝试在npm上使用async-unzip库:https://www.npmjs.com/package/async-unzip

这在内存中工作,并且100%异步,这将为您提供所需的行为=)

以下是一个例子:

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

答案 1 :(得分:0)

我知道这个问题很旧,但是因为我在google上找到了它(这是搜索“异步提取adm-zip”时的第一个结果),并且上面给出的答案没有使用adm-zip而是另一个lib ,我想我应该回答:

Adm-zip具有“ extractAllToAsync”功能(可悲的是在其文档中不存在)。它与extractAllTo基本相同,但是具有一个额外的可选参数(一个回调函数)并且异步工作(根据代码判断,几乎所有工作都是异步的)。

用法:

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