我想了解如何使用Node.js给gzip一些数据...的Node.js - 产卵gzip的过程
具体来说,我在“BUF”的数据,我想要写的这一个压缩的形式“流'。
这里是我的代码:
c1.on('data',function(buf){
var gzip = spawn('gzip', ['-' + (compressionRate-0),'-c', '-']);
gzip.stdin.write(buf);
gzip.stdout.on('data',function(data){
console.log(data);
stream.write(data,'binary');
});
});
麻烦的是,它根本行不通!我不确定产卵过程和管道数据的确切语法。
任何帮助非常感谢。
很多感谢,
编辑:这里是我得到的想法从原来的工作代码。该项目是在:https://github.com/indutny/node.gzip
任何人都可以搞清楚如何做到这一点在node.js产卵因为我完全卡住!
var spawn = require('child_process').spawn,
Buffer = require('buffer').Buffer;
module.exports = function (data) {
var rate = 8,
enc = 'utf8',
isBuffer = Buffer.isBuffer(data),
args = Array.prototype.slice.call(arguments, 1),
callback;
if (!isBuffer && typeof args[0] === 'string') {
enc = args.shift();
}
if (typeof args[0] === 'number') {
rate = args.shift() - 0;
}
callback = args[0];
var gzip = spawn('gzip', ['-' + (rate - 0), '-c', '-']);
var promise = new
process.EventEmitter,
output = [],
output_len = 0;
// No need to use buffer if no
callback was provided
if (callback) {
gzip.stdout.on('data', function (data) {
output.push(data);
output_len += data.length;
});
gzip.on('exit', function (code) {
var buf = new Buffer(output_len);
for (var a = 0, p = 0; p < output_len; p += output[a++].length) {
output[a].copy(buf, p, 0);
}
callback(code, buf);
});
}
// Promise events
gzip.stdout.on('data', function (data) {
promise.emit('data', data);
});
gzip.on('exit', function (code) {
promise.emit('end');
});
if (isBuffer) {
gzip.stdin.encoding = 'binary';
gzip.stdin.end(data.length ? data : '');
} else {
gzip.stdin.end(data ? data.toString() : '', enc);
}
// Return EventEmitter, so node.gzip can be used for streaming
// (thx @indexzero for that tip)
return promise;
};
您可以尝试两件事:设置stdout.on('data'在写入标准输入前,万一出现任何错误,并且在错误信息中没有捕获的情况下侦听stderr。 – StevenGilligan
是的,这是我现在正在做的事情。感谢您的帮助, – Eamorr