I wrote the code below to open a txt file and perform some transformations on it, then write out the transformations to a new file. The original source file though is gzip'd and I can't get the zlib module for node to work. Can someone someone show an example integrating zlib into the example below? In this case. bigdata.sql is actually bigdata.sql.gz, i just gunzip it manually before processing it. Is the answer to make my own object, and inherit the stream object?
I answered my own question, which instead of asking how, perhaps someone can show me a better way to do this? Or maybe this is the best way...
var fs = require('fs');
var util = require('util');
var stream = require('stream');
var zlib = require('zlib');
console.time('gz decompress')
// inherit from the stream object
function transformStream () {
this.writable = true;
this.readable = true;
}
util.inherits(transformStream, stream.Stream);
transformStream.prototype.modifyRow = function (data) {
// console.log("begin data--->");
console.log(data.toString('ascii'));
// console.log("<---end data");
};
transformStream.prototype.write = function(data) {
this.emit("data", data);
};
transformStream.prototype.end = function() {
this.emit("end");
console.timeEnd('gz decompress')
};
transformStream.prototype.destroy = function() {
this.emit('close');
};
var ts = new transformStream();
ts.on("data", function(data) {
this.modifyRow(data);
});
path='bigdata.sql.gz';
var file = fs.createReadStream(path).pipe(zlib.createUnzip()).pipe(ts);