点击(此处)折叠或打开
- #include <zlib.h>
- #include <zconf.h>
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- int main(int argc,char **argv[])
- {
- char msg[1000]="123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n";
- char buf[1000];
- char result[1000];
- unsigned long len, lenresult;
- printf("msg size:%d\n", strlen(msg));
- len = 1000;
- compress(buf, &len, msg, strlen(msg) );
- printf("buf size:%d\n", len);
- lenresult=1000;
- uncompress(result,&lenresult, buf, len );
-
- printf("result size:%d\n", lenresult);
- printf("result:%s\n", result);
- return 0;
- }
编译:
LIB = -lz |
程序中需要注意几个地方:
1. 使用compress 与 uncompress之前,必须为目标长度设初值,而且该值要大于你需要解压或者压缩的结果长度,不然会出问题。
2. 编译需要-lz参数。
方式二:
gzopen(), gzread(), gzwrite(), gzclose()