使用JavaScript / Node解压缩API流?

4
我正在使用node.js与一个返回gzipped数据的API进行交互。我查阅了包管理器和维基百科,寻找一个好的压缩库,但没有找到一个没有被放弃/根本不起作用的库。有什么想法,我可以使用javascript或node来解压缩压缩数据?(或者如何避免数据全部在一起?)
app.get('/', function(req, res){
    // rest is a restler instance
    rest.get('http://api.stackoverflow.com/1.1/questions?type=jsontext', {
            headers: {"Accept-Encoding": 'deflate'}, 
            //tried deflate, gzip, etc. No changes
    }).on('complete', function(data) {
             // If I do: sys.puts(data); I get an exception
             // Maybe I could do something like this:
             /*
             var child = exec("gunzip " + data,
                    function(error, stdout, stderr) {
                            console.log('stdout: ' + stdout);
                            console.log('stderr: ' + stderr);
                             if (error !== null) {
                                    console.log('exec error: ' + error);
                            }
                     }); 
             */
    });

});


相关:https://dev59.com/wlPTa4cB1Zd3GeqPgRoN - j.w.r
1个回答

4
我用这个成功地实现了压缩:

https://github.com/waveto/node-compress

this.get = function(options, cb){
  http.get({host: 'api.stackoverflow.com', path:'/1.1/questions?' + querystring.stringify(vectorize(options || {}))}, function(res){
    var body = [];
    var gunzip = new compress.Gunzip();
    gunzip.init();

    res.setEncoding('binary');
    res
      .on('data', function(chunk){
        body.push(gunzip.inflate(chunk, 'binary'));
      })
      .on('end', function(){
        console.log(res.headers);
        gunzip.end();
        cb(null, JSON.parse(body.join('')));
      });
  }).on('error', function(e){
    cb(e);
  })
}

使用哪个版本的Node?该项目仍然引用了posix(现在是fs),该变更发生在2月份... - Swift
我猜只有示例和文档参考posix。该模块本身只是zlib的一个包装器。我正在使用node v0.4.8。 - Geoff Chappell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接