使用 zlib 进行内存解压
我可以读取内存中的 zlib 压缩文件而不将其实际解压到磁盘吗?如果您能提供一个片段那就太好了。
Can I read a zlib compressed file in memory without actually extracting it to disk? It would be nice if you could provide a snippet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个 zLib inflate 例程,它获取内存中的缓冲区并将其解压缩到提供的输出缓冲区中。这是一个“一次性”函数,因为它尝试一次性填充整个输入缓冲区,并假设您已经为其提供了足够的空间来容纳所有内容。还可以编写一个多镜头函数,根据需要动态增长目标缓冲区。
说明:
src:包含压缩(gzip或zlib)数据的源缓冲区
srcLen:源缓冲区的长度
dst:目标缓冲区,输出将写入其中
dstLen:目标缓冲区的长度
返回值:
Z_BUF_ERROR:如果dstLen不够大,无法容纳膨胀的数据
Z_MEM_ERROR:如果没有足够的内存来执行解压
Z_DATA_ERROR:如果输入数据已损坏
否则,返回值是写入 dst 的字节数。
Here's a zLib inflate routine that takes a buffer in memory and decompresses into the provided output buffer. This is a 'one-shot' function, in that it attempts to inflate the entire input buffer all at once, and assumes you've given it enough space to fit it all. It is also possible to write a multi-shot function that dynamically grows the destination buffer as needed.
Explanation:
src: the source buffer containing the compressed (gzip or zlib) data
srcLen: the length of the source buffer
dst: the destination buffer, into which the output will be written
dstLen: the length of the destination buffer
Return values:
Z_BUF_ERROR: if dstLen is not large enough to fit the inflated data
Z_MEM_ERROR: if there's insufficient memory to perform the decompression
Z_DATA_ERROR: if the input data was corrupt
Otherwise, the return value is the number of bytes written to dst.
Raj Advani 提供的解决方案不适用于多流 zlib 缓冲区。 gzip数据的解决方案:
https://github.com/uvoteam/gunzip_mmap
Solution provided by Raj Advani does not work for multistream zlib buffer. Solution for gzip data:
https://github.com/uvoteam/gunzip_mmap