【问题标题】:How to decode deflate string in nodejs?如何在nodejs中解码deflate字符串?
【发布时间】:2015-04-24 09:39:18
【问题描述】:

我正在使用 nodejs 构建一个应用程序,该应用程序使用一些外部 API,该 API 返回以 deflate 格式压缩的 json 数据,但由于我是 nodejs 新手,所以我查看了 zlib api,但没有找到解码字符串的方法, 请任何人帮助我,这是我的解码字符串:

u�O��0\u0010ſJ�CN4��?RT���ޫʚ�\u0001����f)]��wذڪ�r��7�͓�W�\u0003���?_�\u0005T����\tT����\u000f����y=��cu\u0012��\u000e����A��,5�\u0005\u0017ER�\u0011�^�8\rvE��0x��\u0000>��*�jTRA�\\SQe)/�d�����C�<?�4��4\u0007\b�\u001a��`(��@Y��\u0011{���-j���ք�\u0013� ^Y��T��\u001f��\u0010y�\u001a���=v�\'"�ʒGl��dX\'*��ӞR�b;��Y^���؂\u0017O{��\u001b��\u000fa��\u0018\u0014��8")\u0006m�\u001f�\u0007h����\u000ev96v�7G\u001f�vw��\t��J�i�U\u001a\u0018�(�7����Nζz@�G�p��iȲ,���\u000b\u0004po3��\u0018���\'i�`�(�S�]��P�Eˋ\u0013���\\�\u0007u�\nMЍ5\u0007w~�qhϜ�~E�\u0007OKp\u001f���l\u0003�\u0011~o\u000b����p\u0004m��X]�Uu�\u000b

【问题讨论】:

标签: node.js zlib deflate


【解决方案1】:

不使用任何第三方库,也不尝试检测服务器的编码:

var options = {
  hostname: 'www.example.com',
  port: 80,
  path: '/deflateable-thing',
  method: 'GET'
};

var req = http.request(options, function(res) {
  deflate = zlib.createDeflate();
  res.pipe(deflate);
  deflate.on('data', function (chunk) {
    console.log('data: ' + chunk.toString());
  });
});

req.end();

标准的zlib 库也提供了其他流处理程序,例如gzip

【讨论】:

    【解决方案2】:

    嗯...首先您需要知道... API 生成的是 Raw-Deflate 还是 Deflate。此外,在放气时可以做出许多不同的选择。您必须了解放气时使用的配置。我假设您应该能够在您正在使用的 API 的文档中找到它们。

    让我们暂时假设 API 正在使用默认设置 (https://nodejs.org/api/zlib.html#zlib_options) 生成 Deflate。

    现在...

    var zlib = require( 'zlib' );
    
    // lets assume you have your response from api in a string variable
    var responseDeflateString = "response from api";
    // convert it to a buffer ( as you provided, its a utf8 string)
    var responseDeflateBuffer = new Buffer( responseDeflateString );
    // now inflate the buffer
    // Note 1 : zlib.inflateSync takes another argument "option" and this will
    //          depend upon how this API service was deflating, and should be added 
    //          based on API documentation. For now we are using default options.
    // Note 2 : its better to use the asynchronous version. But we are using
    //          synchronous one for the sake of simplicity.
    var responseInflateBuffer = zlib.inflateSync( responseDeflateBuffer );
    // decode the buffer back to string ( utf8 )
    var responseInflateString = responseInflateBuffer.toString();
    

    【讨论】:

      【解决方案3】:

      Request 支持 gzip 参数。查看请求examples

      var request = require('request')
        request(
          { method: 'GET'
          , uri: 'http://www.google.com'
          , gzip: true
          }
        , function (error, response, body) {
            // body is the decompressed response body
            console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
            console.log('the decoded data is: ' + body)
          }
        )
        .on('data', function(data) {
          // decompressed data as it is received
          console.log('decoded chunk: ' + data)
        })
        .on('response', function(response) {
          // unmodified http.IncomingMessage object
          response.on('data', function(data) {
            // compressed data as it is received
            console.log('received ' + data.length + ' bytes of compressed data')
          })
        })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-18
        • 2013-11-01
        • 2011-03-02
        • 1970-01-01
        • 2016-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多