2014-09-22 91 views
0

我從stdin(使用nodejs)實時解壓縮數據流時遇到問題。 需要處理解壓縮的流,只要它到達stdin(最多幾毫秒的延遲)。 問題是管道stdin zlib接縫等待流關閉。nodejs將管道標準輸入到zlib中而不等待EOF

以下打印12345

$ echo 12345 | node deflate.js | node inflate.js 
12345 

然而下面的命令行不會因爲它沒有接收到EOF:

$ node generator.js | node deflate.js | node inflate.js 

這涉及到的問題,如果zlib的放氣可在內部處理的部分輸入,或應該將它添加到流中(例如每個流塊之前的塊的大小)。

deflate.js:

process.stdin 
.pipe(require("zlib").createDeflate()) 
.pipe(process.stdout); 

inflate.js

process.stdin 
.pipe(require('zlib').createInflate()) 
.pipe(process.stdout) 

generator.js:

var i = 0 
setInterval(function() { 
    process.stdout.write(i.toString()) 
    i++ 
},1000) 

回答

0

的問題是,Z_SYNC_FLUSH標誌沒有設置:

If flush is Z_SYNC_FLUSH, deflate() shall flush all pending output to next_out and align the output to a byte boundary. A synchronization point is generated in the output. 

deflate.js:

var zlib = require("zlib"); 
var deflate = zlib.createDeflate({flush: zlib.Z_SYNC_FLUSH}); 
process.stdin.pipe(deflate).pipe(process.stdout); 

inflate.js:

var zlib = require('zlib') 
process.stdin 
.pipe(zlib.createInflate({flush: zlib.Z_SYNC_FLUSH})) 
.pipe(process.stdout)