在您的代码中使用zlib非常简单,文档(或我发现的stackoverflow上的各种答案)并不明显。
以下技术适用于任何编译器和IDE。我使用代码:blocks在windows mingw中测试它,这就是为什么我将它作为这个问题的答案发布的原因。
从http://www.zlib.net/
下载zlib的源代码复制所有的.c和.h文件从zlib源代码的根文件夹中的文件夹中的编译器搜索路径。
将zlib源文件添加到IDE项目中。
添加#include“zlib。H”到你的源代码
呼叫压缩或解压缩
就是这样。这简直是简单的。
所有你必须要小心的是内存管理,因为这为c 。代码
为了让事情我自己简单的,我已经把一个C++封装程序,欢迎您的使用,像这样:
/** ZLIB C++ wrapper
Usage:
<pre>
#include "cZLIB.h"
{
// compress data in bigbuffer
raven::set::cZLIB ZLIB;
ZLIB.Compress(bigbuffer, sizebigbuffer);
// use compressed buffer, before ZLIB goes out of scope
use(ZLIB.Buffer(), ZLIB.Length());
}
...
{
// decompress data in smallbuffer
raven::set::cZLIB ZLIB;
ZLIB.Inflate(smallbuffer, sizesmallbuffer)
// use decompressed data, before ZLIB goes out of scope
use(ZLIB.Buffer(), ZLIB.Length());
}
</pre>
Build:
Download this code (cZLIB.h and cZLIB.cpp) from
https://github.com/JamesBremner/raven-set
and install somewhere in your compiler search path.
Let's assume you install it in folder .../src.
Download the zlib source code from http://www.zlib.net/
Copy all the .c and .h files from the root folder of the zlib source
to a new folder .../src/zlib
Add the files cZLIB.h, cZLIB.cpp and all the files in .../src/zlib
to the IDE project.
Build.
*/
class cZLIB
...
出于某种原因,我发现mingw有一个zlib库和libz库。当我使用libz时,一切正常。我真的不知道有什么区别,或者为什么这个名字倒退,哦,井。该手册说输出缓冲区只需比输入多12个字节,因为这是最坏情况下的压缩(10个字节的标题和2个字节说没有压缩?)。它现在适用于libz而不是zlib。事实上,我认为zlib库文件是一些文件,这些文件在一段时间之前由于配置错误而意外安装。 (p。要MODS:答案你自己的问题按钮不适合我?!) – myforwik 2010-11-12 09:41:36
@myforwik:libz是zlib。 – 2010-11-12 09:59:54