0

我有一个 node.js 缓冲区实例,其中缓冲区是节的串联,每个节都有一个 20 字节的标头,后跟放气的数据。

我需要的是使用 node.js 读取压缩数据,并知道压缩序列有多少字节,这样我就可以正确地前进到下一个缓冲区部分。像这样的东西:

var zlib = require('zlib');
var sections = [];
// A variable named 'buffer' is declared pointing to the Buffer instance
// so I need to read the first section, then the second section etc.
buffer = buffer.slice(20); // skip the 20-byte header
zlib.inflate(buffer, function(err, inflatedData) {
  sections.push(inflatedData);
});
// How many bytes zlib readed from the buffer to
// create the 'inflatedData' instance?
// suppose the number of bytes read is pointed by the variable 'offset',
// then I could do this to read the next section:
buffer = buffer.slice(offset + 20);
zlib.inflate(buffer, function(err, inflatedData) {
  sections.push(inflatedData);
});
4

2 回答 2

2

NodeJS 文档不清楚。只需添加: { info: true },返回对象将是一个带有解压缩数据的缓冲区和引擎的对象。例如:

const result = zlib.inflateRawSync(compressed, { info: true } as any) as any;
const bytesRead = result.engine.bytesWritten;  // "bytesWritten" is actually "compressedSize".

如果使用 typescript,则需要强制转换为“any”,因为 @types/node@14 尚未指定此选项。

于 2020-09-15T13:15:29.937 回答
0
// How many bytes zlib readed from the buffer to
// create the 'inflatedData' instance?

答:都是。

buffer.slice(20)将从位置 20 直到缓冲区结束。这就是zlib.inflate()方法得到的,所以这就是它处理的。

也许这会有所帮助:https ://meta.stackexchange.com/questions/66377/what-is-the-xy-problem

你似乎很困惑。你到底想做什么?(也就是说,您要解决什么问题?)

于 2013-01-10T01:30:12.627 回答