Node.js - 生成 gzip 进程
我正在尝试使用 Node.js 对一些数据进行 gzip 压缩...
具体来说,我在“buf”中有数据,我想将其 gzip 压缩形式写入“stream”。
这是我的代码:
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;
};
I'm trying to gzip some data using Node.js...
Specifically, I have data in 'buf' and I want to write a gzipped form of this to 'stream'.
Here is my code:
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');
});
});
The trouble is, it simply won't work! I'm not sure of the exact syntax for spawning processes and piping data to them.
Any help greatly appreciated.
Many thanks in advance,
Edit: here is the original working code where I got the idea from. The project is at: https://github.com/indutny/node.gzip
Can anyone work out how to do this spawning in node.js cos I'm totally stuck!
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;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不简单地使用您“受到启发”的 gzip 节点库而不是复制代码呢?
应该使用图书馆工作。要安装它,只需在终端中输入
npm install gzip
即可。Why don't you simply use the gzip node library that you are "inspired from" instead of copying the code?
Should work using the library. To install it simply type
npm install gzip
in your terminal.您需要在 gzip.stdin 上调用“end”方法吗? IE:
Do you need to call the 'end' method on gzip.stdin? I.e.: