Tuesday 29 January 2019

How to compress a large string with gzip to a file?

Hello, I was wondering what is the proper way to write data from in-memory to file and have it compressed along the way? The best I got is this. I get "Error: write after end" most of the time (not always).const stream = require('stream'); const zlib = require('zlib'); const gzip = zlib.createGzip({level: 9}); let Duplex = stream.Duplex; const stream = new Duplex(); stream.push(JSON.stringify(data)); stream.push(null); const inp = stream; const out = fs.createWriteStream('./foo.json.gz'); inp.pipe(gzip).pipe(out); I tried with a stream.passThrough as the input (it wants a readable) and got the same result.Thank you :)

Submitted January 30, 2019 at 03:11AM by charlesdarkwind

No comments:

Post a Comment