kev*_*ler 6 javascript node.js
我似乎无法让它发挥作用。我想将一些数据写入 gzip 流,然后将 gzip 流通过管道传输到文件写入流。我想在文件写入完成后调用一个函数。我目前有:
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();
});
Run Code Online (Sandbox Code Playgroud)
永远不会调用完成事件或错误事件。
尝试
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();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7781 次 |
| 最近记录: |