1

I want to zip a few readeableStreams into a writableStream. the purpose is to do all in memory and not to create an actual zip file on disk.

for that i'm using archiver

        let bufferOutput = Buffer.alloc(5000);
        let archive = archiver('zip', {
            zlib: { level: 9 } // Sets the compression level.
        });
        archive.pipe(bufferOutput);
        archive.append(someReadableStread, { name: test.txt});
        archive.finalize();

I get an error on line archive.pipe(bufferOutput);.

This is the error: "dest.on is not a function"

what am i doing wrong? Thx

UPDATE:

I'm running the following code for testing and the ZIP file is not created properly. what am I missing?

const   fs = require('fs'),
    archiver = require('archiver'),
    streamBuffers = require('stream-buffers');

let outputStreamBuffer = new streamBuffers.WritableStreamBuffer({
    initialSize: (1000 * 1024),   // start at 1000 kilobytes.
    incrementAmount: (1000 * 1024) // grow by 1000 kilobytes each time buffer overflows.
});

let archive = archiver('zip', {
    zlib: { level: 9 } // Sets the compression level.
});
archive.pipe(outputStreamBuffer);

archive.append("this is a test", { name: "test.txt"});
archive.finalize();

outputStreamBuffer.end();

fs.writeFile('output.zip', outputStreamBuffer.getContents(), function() { console.log('done!'); });
Yoni Mayer
  • 1,212
  • 1
  • 14
  • 27

4 Answers4

4

In your updated example, I think you are trying to get the contents before it has been written.

Hook into the finish event and get the contents then.

outputStreamBuffer.on('finish', () => {
  // Do something with the contents here
  outputStreamBuffer.getContents()
})
patmood
  • 1,565
  • 1
  • 12
  • 15
2

By adding the event listener on archiver works for me:

archive.on('finish', function() {
   outputStreamBuffer.end();
   // write your file
});
Z Li
  • 21
  • 1
1

A Buffer is not a stream, you need something like https://www.npmjs.com/package/stream-buffers

Dave Irvine
  • 384
  • 4
  • 14
1

As for why you are seeing garbage, this is because what you are seeing is the zipped data, which will seem like garbage.

To verify if the zipping has worked, you probably want to unzip it again and check if the output matches the input.

Dave Irvine
  • 384
  • 4
  • 14
  • i just noticed that it actually get stuck on "outputStreamBuffer.getContents().pipe(output);" and doesn't procced – Yoni Mayer Jul 19 '17 at 12:33
  • 1
    Ah, because, again, `outputStreamBuffer.getContents()` gives you a Buffer not a Stream, so it has no `pipe()` function. Just do `fs.writeFile('output.zip', outputStreamBuffer.getContents(), function() { console.log('done!'); });` – Dave Irvine Jul 19 '17 at 12:37
  • umm... still not working for me. i've updated my question if you can have a look. thx a lot for your help – Yoni Mayer Jul 20 '17 at 13:44