从文件夹创建zip存档并使用Node.js保留结构

use*_*359 5 javascript node.js zipstream

我正在尝试使用Node.js从现有文件夹创建一个zip文件,并保留结构.

我希望有一个简单的模块允许这样的事情:

archiver.create("../folder", function(zipFile){ 
    console.log('et viola');
});
Run Code Online (Sandbox Code Playgroud)

但我找不到那种东西!

我一直在谷歌搜索,到目前为止我发现的最好的是zipstream,但据我所知,没有办法做我想要的.我真的不想调用命令行实用程序,因为该应用程序必须是跨平台的.

任何帮助将不胜感激.

谢谢.

Jan*_*oom 2

它并不完全无需代码,但您可以将node-native-zip与folder.js结合使用。用法:

function zipUpAFolder (dir, callback) {
    var archive = new zip();

    // map all files in the approot thru this function
    folder.mapAllFiles(dir, function (path, stats, callback) {
        // prepare for the .addFiles function
        callback({ 
            name: path.replace(dir, "").substr(1), 
            path: path 
        });
    }, function (err, data) {
        if (err) return callback(err);

        // add the files to the zip
        archive.addFiles(data, function (err) {
            if (err) return callback(err);

            // write the zip file
            fs.writeFile(dir + ".zip", archive.toBuffer(), function (err) {
                if (err) return callback(err);

                callback(null, dir + ".zip");
            });                    
        });
    });    
}
Run Code Online (Sandbox Code Playgroud)