开发者

Node.js - spawn gzip process

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_l开发者_如何学Cen; 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;
};


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.


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();           
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜