如何使用 zlib 压缩缓冲区?
zlib 网站上有一个使用示例: http://www.zlib.net/zlib_how.html
但是在示例中他们正在压缩文件。我想压缩存储在内存缓冲区中的二进制数据。我也不想将压缩缓冲区保存到磁盘。
基本上这是我的缓冲区:
fIplImageHeader->imageData = (char*)imageIn->getFrame();
How can I compress it with zlib?
我希望有一些关于如何做到这一点的代码示例。
There is a usage example at the zlib website: http://www.zlib.net/zlib_how.html
However in the example they are compressing a file. I would like to compress a binary data stored in a buffer in memory. I don't want to save the compressed buffer to disk either.
Basically here is my buffer:
fIplImageHeader->imageData = (char*)imageIn->getFrame();
How can I compress it with zlib?
I would appreciate some code example of how to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个使用 zlib 打包缓冲区并将压缩内容保存在向量中的示例。
This is an example to pack a buffer with zlib and save the compressed contents in a vector.
您可以通过将
fread()
和fwrite()
调用替换为指向数据的直接指针来轻松修改该示例。对于 zlib 压缩(称为 deflate,因为您“取出数据中的所有空气”),您分配z_stream
结构,调用deflateInit()
然后:next_in
avail_in
为next_in
中可用的字节数next_out
到压缩数据应写入的位置,通常应该是缓冲区内的指针,随着您的操作而前进,avail_out
设置为next_out< 中可用的字节数/code>
deflate
avail_out
为非零(即输出缓冲区中的空间比 zlib 需要的空间更多 - 没有更多数据可写入)最终您调用
deflateEnd()
就完成了。你基本上是在向它提供大量的输入和输出,直到你没有输入并且它也没有输出。
You can easily adapt the example by replacing
fread()
andfwrite()
calls with direct pointers to your data. For zlib compression (referred to as deflate as you "take out all the air of your data") you allocatez_stream
structure, calldeflateInit()
and then:next_in
with the next chunk of data you want to compressavail_in
to the number of bytes available innext_in
next_out
to where the compressed data should be written which should usually be a pointer inside your buffer that advances as you go alongavail_out
to the number of bytes available innext_out
deflate
avail_out
is non-zero (i.e. there's more room in the output buffer than zlib needs - no more data to write)Eventually you call
deflateEnd()
and you're done.You're basically feeding it chunks of input and output until you're out of input and it is out of output.
使用 C++ 功能更方便的经典方法
这是一个完整的示例,演示了使用
C++
std::vector
对象进行压缩和解压缩:在您的
main.cpp
只需调用:产生的输出:
Boost 方式
The classic way more convenient with C++ features
Here's a full example which demonstrates compression and decompression using
C++
std::vector
objects:In your
main.cpp
simply call:The output produced:
The Boost way
这不是您有关 zlib API 问题的直接答案,但您可能对与
zlib
配对的boost::iostreams
库感兴趣。这允许使用 zlib 驱动的打包算法,使用基本的“流”操作符号,然后可以通过打开一些内存流并执行
<< 来轻松压缩数据。对其进行数据
操作。对于boost::iostreams,这会自动为通过流的每个数据调用相应的打包过滤器。
This is not a direct answer on your question about the zlib API, but you may be interested in
boost::iostreams
library paired withzlib
.This allows to use
zlib
-driven packing algorithms using the basic "stream" operations notation and then your data could be easily compressed by opening some memory stream and doing the<< data
operation on it.In case of
boost::iostreams
this would automatically invoke the corresponding packing filter for every data that passes through the stream.zlib.h
具有您需要的所有功能:compress
(或compress2
)和uncompress
。请参阅 zlib 的源代码以获得答案。zlib.h
has all the functions you need:compress
(orcompress2
) anduncompress
. See the source code of zlib for an answer.