This article mainly introduces how to use archiver in the packaging module of nodejs, which is very simple and practical. We recommend it to partners who need it. Archiver is a module that supports cross-platform packaging in nodejs. It can be zip and tar packages. It is a relatively easy-to-use third-party module.
Install the archiver module before use.
The Code is as follows:
Npm install archiver
Create a piece of code
The Code is as follows:
Var archiver = require ('archiveer ');
Var fs = require ('fs ');
// Package the file
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 an archiver object. The package 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 archiver object
ZipArchiver. append (fs. createReadStream (files [I]), {'name': files [I]});
}
// Package
ZipArchiver. finalize ();
It is very simple to complete the packaging function.
This module: https://github.com/ctalkington/node-archiver