使用 zlib 将字节数组解压缩为字节数组

Decompressing a byte array with zlib to a byte array

上下文:我正在使用 .mbtiles 文件,这是一种地理映射文件格式,它是一个包含矢量切片的 sqlite 数据库文件。

这些矢量切片使用协议缓冲区进行打包,然后进行 gzip 压缩。

我正在使用 C++,目前正在阅读 zlib 用法解压缩示例,但我不确定如何处理块和流结束事件。

SQLite 给了我一个 void* 指针和一个长度。

我引用页面:

For applications where zlib streams are embedded in other data, this routine would need to be modified to return the unused data, or at least indicate how much of the input data was not used, so the application would know where to pick up after the zlib stream.

协议缓冲区 class 方法采用 void* 或 std::string。我想我应该选择 void*.

我不确定这些事件是如何工作的,而且该示例似乎没有提供字节数组的案例。我应该如何更改代码以避免错误?

听起来 SQLite 正在给你一个 zlib 流,后面没有任何内容。如果是,则该评论不适用。

无论如何,您正在查看 the right page。 (你没有说 "the page" 是什么,但我认得这句话,因为它是我写的。)这说明了如何使用 zlib 函数。您应该能够弄清楚如何将它应用于字节数组而不是文件输入。

如果数据真的是"gzipped",那么您将需要使用inflateInit2()而不是inflateInit()。阅读 zlib documentation in zlib.h.