node.js - Nodejs异步提取zip

标签 node.js zip adm-zip

我目前正在使用 adm-zip 将 zip 文件提取到特定路径,但其 extractAllTo 方法是同步的。

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

最佳答案

尝试在 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);
});

关于node.js - Nodejs异步提取zip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28099037/

相关文章:

node.js - 在 typescript 上扩展 super 测试

node.js - vue + vite + ubuntu 20 个错误

node.js - sequelize nodejs 我可以用 1 个 findAll 找到自定义数组中的所有电子邮件吗?

php - 创建zip时没有错误,但没有创建

c# - SendToZip 和 C# CreateFromDirectory zipfile 之间的区别

node.js - 在 MongooseJS 中对 `open` 事件使用 on() 或 once()

php - PHP 中的压缩流

Powershell Write-Zip 部分文件夹树

javascript - Adm Zip - 文件压缩为文件夹

node.js - nodejs从url下载并解压文件,错误No END header found