Archiver is a module that can realize cross-platform packaging function in nodejs. It can make zip and tar packages. It is a relatively easy-to-use third-party module.
Install the archive module before use.
npm install archiver
Create a piece of code
var archiver = require('archiver');
var fs = require('fs');
//Packed file
var files = [
'files/001.png',
'files/002.png'
];
var zipPath = 'test.zip';
//Create an output stream for the final packaged file
var output = fs.createWriteStream(zipPath);
//Generate archive object, the packaging type is zip
var zipArchiver = archiver('zip');
//Associate the packaging object with the output stream
zipArchiver.pipe(output);
for(var i=0; i < files.length; i ) {
console.log(files[i]);
//Add the stream of the packaged file to the archive object
zipArchiver.append(fs.createReadStream(files[i]), {'name': files[i]});
}
//Package
zipArchiver.finalize();
It is very simple to complete the packaging function.
Download address of this module: https://github.com/ctalkington/node-archiver