Nodejs packaging module archiver details

  • 2020-03-30 04:26:51
  • OfStack

Archiver is a cross-platform module in nodejs, which can be used for zip and tar packages.

Install the archiver module before using it.


npm install archiver

Create a piece of code


var archiver = require('archiver');
var fs = require('fs');
//Packaged file
var files = [
  'files/001.png',
  'files/002.png'
  ];
var zipPath = 'test.zip';
//Creates an output stream of the final packaged file
var output = fs.createWriteStream(zipPath);
//Generate the archiver object, packaged with 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]);
  //Adds the stream of the packaged file to the archiver object
  zipArchiver.append(fs.createReadStream(files[i]), {'name': files[i]});
}
//Packaging < br / > zipArchiver.finalize();

Very simple to complete the packaging function.

This module download address: (link: https://github.com/ctalkington/node-archiver)


Related articles: