Can I get the Buffer from fs.createWriteStream()?

3.2k views Asked by At

I have simple question - Can I get the Buffer from fs.createWriteStream()?

In my case I use archiver NPM module which may create archives from Buffer. When this module end work with method archive.finalize() I may get result from archive.pipe(stream). I want have Buffer from fs.createWriteStream() which I may send to res.send() Express method.

let archive = archiver("zip", {
      zlib: { level: 9 } // Sets the compression level.
    });
const stream = fs.createWriteStream(
      "./src/backend/api/test.zip"
    );
archive.pipe(stream);
archive.append(buffer, { name: "test.csv" });
archive.finalize();
1

There are 1 answers

0
Interface On

If I understood your problem correctly, that might help you

        const stream = fs.createWriteStream(
                "./src/backend/api/test.zip"
            );

        stream.on('data', (chunk) => {
            //here u get every chunk as a buffer  
        })
        stream.on('finish', () => {
            //here your code if the stream is ending
        })