如何使用nodejs的zlib从.net解压GzipStream?

问题描述 投票:0回答:1

传入的数据是来自某人的发布请求的二进制格式。我们知道它是由以下创建的:https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.gzipstream?view=netframework-4.6

body: `\x1F�\b\x00\x00\x00\x00\x00\x04\x00��]o�0\x14���oG��\x0F\x02��t�&�\x1B"\x19�6��I��7'����\x10�}v\n` +`\x1F�\b\x00\x00\x00\x00\x00\x04\x00��]o�0\x14���oG��\x0F\x02��t�&�\x1B"\x19�6��I��7'����\x10�}v\n` 
    ....,
  isBase64Encoded: false
...
'Content-Type': 'application/gzip',

我目前的尝试

const B = new Uint8Array(Buffer.from(body, 'binary'));
// or const B = Buffer.from(body, 'binary'); not work

zlib.unzip(B, (err, buffer) => {
  if (!err) {
    console.log(buffer.toString());
  } else {
    console.log(err);
  }
});

并且犯了一些错误:

Error: incorrect header check
    at Zlib.zlibOnError [as onerror] (node:zlib:189:17) {
  errno: -3,
  code: 'Z_DATA_ERROR'
}

预计

解压此数据并使用 Nodejs 中的

zlib
解析为 json。

javascript node.js .net stream gzip
1个回答
0
投票

尝试使用

zlib.gunzip
。您的数据是 gzip 流,而不是 zlib 流。

© www.soinside.com 2019 - 2024. All rights reserved.