1. 如何获得zlib
zlib的主页是:http://www.zlib.net/
2. 用VC++6.0打开
把下载的源代码解压打开,VC6.0的工程已经建好了,在/projects/visualc6. 双击zlib.dsw, 可以在VC++6.0中看到里面有3个工程: zlib 是库文件(编译设置选中 win32 lib debug / release), 工程example 是如何使用 zlib.lib 的示例, 工程minigzip 是如何用 zlib 提供的函数读写.gz文件的示例(*.gz的文件一般Linux下比较常用).
3. 如何加入到我的工程
编译好 zlib.lib 后, 你就得到了调用一个静态库所需要的所有文件了(zlib.lib, zlib.h, zconf.h). 如何调用静态库不用我说了吧.
4. 用zlib能干什么
先来看看 zlib 都提供了那些函数, 都在zlib.h中,看到一堆宏不要晕,其实都是为了兼容各种编译器和一些类型定义.死死抓住那些主要的函数的原型声明就不会受到这些东西的影响了.
关键的函数有那么几个:
(1)int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
把源缓冲压缩成目的缓冲, 就那么简单, 一个函数搞定
(2) int compress2 (Bytef *dest, uLongf *destLen,const Bytef *source, uLong sourceLen,int level);
功能和上一个函数一样,都一个参数可以指定压缩质量和压缩数度之间的关系(0-9)不敢肯定这个参数的话不用太在意它,明白一个道理就好了: 要想得到高的压缩比就要多花时间
(3) uLong compressBound (uLong sourceLen);
计算需要的缓冲区长度. 假设你在压缩之前就想知道你的产度为 sourcelen 的数据压缩后有多大, 可调用这个函数计算一下,这个函数并不能得到精确的结果,但是它可以保证实际输出长度肯定小于它计算出来的长度
(4) int uncompress (Bytef *dest, uLongf *destLen,const Bytef *source, uLong sourceLen);
解压缩(看名字就知道了:)
(5) deflateInit() + deflate() + deflateEnd()
3个函数结合使用完成压缩功能,具体用法看 example.c 的 test_deflate()函数. 其实 compress() 函数内部就是用这3个函数实现的(工程 zlib 的 compress.c 文件)
(6) inflateInit() + inflate() + inflateEnd()
和(5)类似,完成解压缩功能.
(7) gz开头的函数. 用来操作*.gz的文件,和文件stdio调用方式类似. 想知道怎么用的话看example.c 的 test_gzio() 函数,很easy.
函数宣告
gzFile gzopen (const char *path, const char *mode);
开启一个gzip(*.gz)档。
mode参数可为"rb"或"wb"。
另外也可包含压缩程度如"wb9"。
用'f'作为过滤资料,如"wb6f"。
用'h'可指定Huffman only压缩,如"wb1h"
gzopen亦可用於读取非压缩的gzip档案格式,在这种状况下,gzread会直接读取,而不进行解压缩。
int gzread (gzFile file, voidp buf, unsigned len);
与read的用法相同。
int gzwrite (gzFile file, const voidp buf, unsigned len);
与write用法相同。
int gzprintf (gzFile file, const char *format, ...);
与fprintf用法相同。
char * gzgets (gzFile file, char *buf, int len);
与fgets用法相同。
int gzputc (gzFile file, int c);
与fputc用法相同。
int gzgetc (gzFile file);
与fgetc用法相同。
int gzflush (gzFile file, int flush);
与fflush作用相同。
z_off_t gzseek (gzFile file, z_off_t offset, int whence);
whence不支援SEEK_END
如果档案是开启为"读取",则SEEK_SET及SEEK_CUR,向前及向後均支援,不过很慢就是了。
如果档案是开启为"写入",仅支援向前SEEK。
int gzrewind (gzFile file);
与gzseek(file, 0L, SEEK_SET)相同作用,仅在读取时有效。
z_off_t gztell (gzFile file);
返回值 : 目前档案位置(解压缩後的位置)
int gzeof (gzFile file);
返回值 : 1 - EOF, 0 - not EOF
int gzclose (gzFile file);
关闭档案
返回值 : zlib error number
(8) 其他诸如获得版本等函数就不说了.
总结: 其实只要有了compress() 和uncompress() 两个函数,在大多数应用中就足够了.
使用zlib库的静态lib(zlib.lib)与主程序进行(静态)连接时,必须在"代码生成"中选中"运行时库时"必须选择
"多线程调试(/MTd)"或"多线程(/MT)"
然后在连接选项中忽略msvcrt.lib.
否则出现链接错误.
LNK2005 libcmt.lib和msvcrt.lib冲突
注意保持整个工程在"代码生成"页面的"运行时库时"(/MT,/MTd,/MD,/MDd)的一致性.
在一个使用zilb的工程中(该工程所有代码要求静态链接),发现最后进行exe链接生成时碰到了链接错误.
要么是error LNK2001,要么是 LNK2005.
经检查主要原因是因为运行时选择出了问题.现总结经验如下.供以后参考.
使用zlib库的静态lib(zlib.lib)与主程序进行(静态)连接时,必须在"代码生成"中选中"运行时库时"必须选择
"多线程调试(/MTd)"或"多线程(/MT)"
然后在连接选项中忽略msvcrt.lib.
否则出现链接错误:LNK2005 libcmt.lib和msvcrt.lib冲突
注意保持整个工程在"代码生成"页面的"运行时库时"(/MT,/MTd,/MD,/MDd)的一致性.
另外的方法是:使用/FORCE:MULTIPLE 链接选项.这样可以忽略掉这个链接错误(转换为warning).
个人觉得还是使用上面的方法比较保险一些.这个(/FORCE:MULTIPLE)方法作为备用方案.
#############参数类型一定要正确,不然会出现莫名其妙的错误,如Z_BUF_ERROR
#############切记!!!!!!!!!!!!!!!!!
#############uLong comprLen, uncomprLen!!!
// testzlib.cpp 简单测试 zlib 的压缩功能
#include <cstring>
#include <cstdlib>
#include <iostream>
#include "zlib.h"
using namespace std;
int main()
{
int err;
Byte compr[200], uncompr[200]; // big enough
uLong comprLen, uncomprLen;
const char* hello = "12345678901234567890123456789012345678901234567890";
uLong len = strlen(hello) + 1;
comprLen = sizeof(compr) / sizeof(compr[0]);
err = compress(compr, &comprLen, (const Bytef*)hello, len);
if (err != Z_OK) {
cerr << "compess error: " << err << '/n';
exit(1);
}
cout << "orignal size: " << len
<< " , compressed size : " << comprLen << '/n';
strcpy((char*)uncompr, "garbage");
err = uncompress(uncompr, &uncomprLen, compr, comprLen);
if (err != Z_OK) {
cerr << "uncompess error: " << err << '/n';
exit(1);
}
cout << "orignal size: " << len
<< " , uncompressed size : " << uncomprLen << '/n';
if (strcmp((char*)uncompr, hello)) {
cerr << "BAD uncompress!!!/n";
exit(1);
} else {
cout << "uncompress() succeed: /n" << (char *)uncompr;
}
}
#include "stdafx.h"
#include<string.h>
#include"zconf.h"
#include "zlib.h"
#pragma comment(lib, "zlib1d.lib")
int main()
{
//原始数据
const unsigned char strSrc[]="hello world!/n/
aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试/
aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试/
aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试/
aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试";
unsigned char buf[1024]={0},strDst[1024]={0};
unsigned long srcLen=sizeof(strSrc),bufLen=sizeof(buf),dstLen=sizeof(strDst);
printf("Src string:%s/nLength:%d/n",strSrc,srcLen);
//压缩
compress(buf,&bufLen,strSrc,srcLen);
printf("/nAfter Compressed Length:%d/n",bufLen);
printf("Compressed String:%s/n",buf);
//解压缩
uncompress(strDst,&dstLen,buf,bufLen);
printf("/nAfter UnCompressed Length:%d/n",dstLen);
printf("UnCompressed String:%s/n",strDst);
return 0;
}