Archiver is a module that can implement packaging functions across platforms in nodejs. It can use zip and tar packages. It is a relatively easy-to-use three-party module.
Install the archiver module before use.
The code copy is as follows:
npm install archiver
Create a piece of code
The code copy is as follows:
var archiver = require('archiver');
var fs = require('fs');
//The file is packaged
var files = [
'files/001.png',
'files/002.png'
];
var zipPath = 'test.zip';
//Create an output stream of the final packaged file
var output = fs.createWriteStream(zipPath);
// Generate archiver object, package type zip
var zipArchiver = archiver('zip');
//Associate the packaged 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 into the archiver object
zipArchiver.append(fs.createReadStream(files[i]), {'name': files[i]});
}
//Pack
zipArchiver.finalize();
Very simple to complete the packaging function.
Download address of this module: https://github.com/ctalkington/node-archiver