Node.js - 生成 gzip 进程

发布于 2024-11-17 19:01:20 字数 2378 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-11-24 19:01:20

为什么不简单地使用您“受到启发”的 gzip 节点库而不是复制代码呢?

var gzip = require('gzip');
c1.on('data' function(buf){
    gzip(buf, function(err, data){
        stream.write(data, 'binary');
    }
}

应该使用图书馆工作。要安装它,只需在终端中输入 npm install gzip 即可。

Why don't you simply use the gzip node library that you are "inspired from" instead of copying the code?

var gzip = require('gzip');
c1.on('data' function(buf){
    gzip(buf, function(err, data){
        stream.write(data, 'binary');
    }
}

Should work using the library. To install it simply type npm install gzip in your terminal.

A君 2024-11-24 19:01:20

您需要在 gzip.stdin 上调用“end”方法吗? IE:

gzip.stdin.write(buf);
gzip.stdout.on('data',function(data){
        console.log(data);
        stream.write(data,'binary');
});
gzip.stdin.end();           

Do you need to call the 'end' method on gzip.stdin? I.e.:

gzip.stdin.write(buf);
gzip.stdout.on('data',function(data){
        console.log(data);
        stream.write(data,'binary');
});
gzip.stdin.end();           
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文