ksoichiro / node-archiver-zip-encryptable

An extension for archiver to zip with password encryption.
MIT License
12 stars 1 forks source link

Buffer #5

Closed atodhruv closed 5 years ago

atodhruv commented 5 years ago

HelpNeeded How can I get buffer of zip instead of store file in local drive.

Thanks in advance

ksoichiro commented 5 years ago

Sorry for my late reply. I think this is not a issue of this library, but you here is a sample code:

var fs = require('fs');
var archiver = require('archiver');
var passThrough = require('stream').PassThrough();

archiver.registerFormat('zip-encryptable', require('archiver-zip-encryptable'));

var archive = archiver('zip-encryptable', {
  zlib: { level: 9 },
  forceLocalTime: true,
  password: 'test'
});
archive.pipe(passThrough);

archive.append(Buffer.from('Hello World'), { name: 'test.txt' });

var zip = Buffer.alloc(0);
passThrough.on('readable', function() {
  while (data = this.read()) {
    zip = Buffer.concat([zip, data], zip.length + data.length);
  }
});

archive.finalize()
  .then(function() {
    passThrough.end();
    // You can get zip as Buffer
    console.log(zip.length);
  });