从当前文件夹压缩应用程序

Zip application from the current folder

我有 node.js 个应用程序,我需要使用以下命令压缩所有当前文件夹 并在根

上获取 zip

为此,我想使用 archiver npm 包,但我不明白以下内容:

  1. where I put the current folder (since I want to zip all the application )
  2. where should I put the name of the zip (the zip that should be created when execute the command)

我的应用程序具有以下结构

MyApp
  Node_modules
  server.js
  app.js 
  package.json
  arc.js

arc.js 中,我已经放置了所有 zip 逻辑,所以我想我需要提供 zipPath(在我的例子中是“./”) 和 myZip 之类的 zip 名称......

我尝试了以下但没有成功,知道吗?

var fs = require('fs');
var archiver = require('archiver');

// create a file to stream archive data to.
var output = fs.createWriteStream(__dirname + '/.');
var archive = archiver('zip', {
    zlib: { level: 9 } // Sets the compression level.
});

// listen for all archive data to be written
output.on('close', function() {
    console.log(archive.pointer() + ' total bytes');
    console.log('archiver has been finalized and the output file descriptor has closed.');
});

archive.on('warning', function(err) {
    if (err.code === 'ENOENT') {
        // log warning
    } else {
        // throw error
        throw err;
    }
});

// good practice to catch this error explicitly
archive.on('error', function(err) {
    throw err;
});

// pipe archive data to the file
archive.pipe(output);

当我打开命令行时需要它

folder->myApp-> 运行 zip arc 并将在当前路径下创建压缩文件(在本例中为根目录....)

github of node-archiver there is an example folder



where I put the current folder (since I want to zip all the application )

示例:

var file1 = __dirname + '/fixtures/file1.txt';
var file2 = __dirname + '/fixtures/file2.txt';

archive
  .append(fs.createReadStream(file1), { name: 'file1.txt' })
  .append(fs.createReadStream(file2), { name: 'file2.txt' })
  .finalize();

具体关于您的案例和目录,您可以使用 .directory() method of archiver


where should I put the name of the zip (the zip that should be created when execute the command)

示例:

var output = fs.createWriteStream(__dirname + '/example-output.zip');

您可以使用 glob 方法,但一定要排除 *.zip 文件。否则 zip 文件本身将成为存档的一部分。 这是一个例子:

// require modules
var fs = require('fs');
var archiver = require('archiver');

// create a file to stream archive data to.
var output = fs.createWriteStream(__dirname + '/example.zip');
var archive = archiver('zip', {
    zlib: { level: 9 } // Sets the compression level.
});

// listen for all archive data to be written
output.on('close', function () {
    console.log(archive.pointer() + ' total bytes');
    console.log('archiver has been finalized and the output file descriptor has closed.');
});

// good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', function (err) {
    if (err.code === 'ENOENT') {
        // log warning
    } else {
        // throw error
        throw err;
    }
});

// good practice to catch this error explicitly
archive.on('error', function (err) {
    throw err;
});

// pipe archive data to the file
archive.pipe(output);

archive.glob('**/*', { ignore: ['*.zip'] });

archive.finalize();