I can't seem to get this to work. I want to write some data to a gzip stream and then pipe the gzip stream to a file write stream. I want to then call a function when the files done writing. I currently have:
var gz = zlib.createGzip()
.pipe(fs.createWriteStream(gz_path));
gz.write(data);
gz.on('error', function(err){
console.log("wtf error", err);
});
gz.on('finish', function(){
console.log("write stream is done");
dosomething();
});
The finish event or the error event is never called.
try
var zlib = require('zlib');
var stream = require('stream');
var util = require('util');
var fs = require('fs');
var gz = zlib.createGzip();
function StringifyStream(){
stream.Transform.call(this);
this._readableState.objectMode = false;
this._writableState.objectMode = true;
}
util.inherits(StringifyStream, stream.Transform);
StringifyStream.prototype._transform = function(obj, encoding, cb){
this.push(JSON.stringify(obj));
cb();
};
var data = "some data in here";
var rs = new stream.Readable({ objectMode: true });
rs.push(data);
rs.push(null);
rs.pipe(new StringifyStream())
.pipe(gz)
.pipe(fs.createWriteStream('test.gz'))
.on('error', function(err){
console.log("wtf error", err);
})
.on('finish', function(){
console.log("write stream is done");
// dosomething();
});