2

I'm using NodeJs (expressJs) and archiver module to stream zip file to the client, so the final ZIP file do not existe in server storage (streaming zip serving), but the probleme is that i can't get the size of the output file because the zip process happen in real time.

Code :

var archiver = require('archiver');
var files = ["file1","file2"];
var zipper = archiver('zip');

zipper.on('error', function(err) {
res.status(500).send({
    error: err.message
});
});

res.attachment('output.zip');
res.setHeader('Content-Type', 'application/zip');
zipper.pipe(res);
files.forEach(f => {
zipper.file(f, {
    name: f
});
});

zipper.finalize();
AHmedRef
  • 2,555
  • 12
  • 43
  • 75

1 Answers1

1

You have listen for the "data" event on the readable stream. Here zipper is a readable stream,so you can calculate the whole size by adding the size of each chunk.

var sizeOfZipFile = 0;
zipper.on('data', (chunk) => {
 sizeOfZipFile += chunk.length 
});
console.log(sizeOfZipFile) //logs the size of the zip file
Sumanth Reddy
  • 134
  • 1
  • 7
  • but the size info will be available before stating download file ? – AHmedRef Feb 19 '18 at 15:37
  • we can't find the full size of streams ahead without a workaround.There is an npm module that usually works.Check the link https://www.npmjs.com/package/stream-length – Sumanth Reddy Feb 19 '18 at 16:19
  • I can help if you share some runnable code with comments – Sumanth Reddy Feb 25 '18 at 07:44
  • Any further development on this? I was also looking for a way of predicting file size without actually performing the write eg. compress in memory and if size exceeds certain size the don't write. – MikeB Jan 30 '19 at 20:25