- 浏览: 151353 次
- 性别:
- 来自: 北京
文章分类
最新评论
zlib库剖析(4):使用示例example.c
下面分析test/example.c,它示范了zlib库的各个函数的使用。
下面分析test/example.c,它示范了zlib库的各个函数的使用。下面代码定义要压缩的字符串、压缩时使用的字典、压缩/解压缩的内存分配策略等。
- /*example.c--usageexampleofthezlibcompressionlibrary
- *Copyright(C)1995-2006,2011Jean-loupGailly.
- *Forconditionsofdistributionanduse,seecopyrightnoticeinzlib.h
- */
- /*@(#)$Id$*/
- #include"zlib.h"
- #include<stdio.h>
- #ifdefSTDC
- #include<string.h>
- #include<stdlib.h>
- #endif
- #ifdefined(VMS)||defined(RISCOS)
- #defineTESTFILE"foo-gz"
- #else
- #defineTESTFILE"foo.gz"
- #endif
- #defineCHECK_ERR(err,msg){\
- if(err!=Z_OK){\
- fprintf(stderr,"%serror:%d\n",msg,err);\
- exit(1);\
- }\
- }
- constcharhello[]="hello,hello!";/*字符长度为14(末尾还有一个null字符)*/
- /*"helloworld"wouldbemorestandard,buttherepeated"hello"
- *stressesthecompressioncodebetter,sorry...
- */
- constchardictionary[]="hello";
- uLongdictId;/*字典的Adler32校验值*/
- voidtest_deflateOF((Byte*compr,uLongcomprLen));
- voidtest_inflateOF((Byte*compr,uLongcomprLen,
- Byte*uncompr,uLonguncomprLen));
- voidtest_large_deflateOF((Byte*compr,uLongcomprLen,
- Byte*uncompr,uLonguncomprLen));
- voidtest_large_inflateOF((Byte*compr,uLongcomprLen,
- Byte*uncompr,uLonguncomprLen));
- voidtest_flushOF((Byte*compr,uLong*comprLen));
- voidtest_syncOF((Byte*compr,uLongcomprLen,
- Byte*uncompr,uLonguncomprLen));
- voidtest_dict_deflateOF((Byte*compr,uLongcomprLen));
- voidtest_dict_inflateOF((Byte*compr,uLongcomprLen,
- Byte*uncompr,uLonguncomprLen));
- intmainOF((intargc,char*argv[]));
- /*Z_SOLO表示把zlib库编译成单独的不依赖第三方的库*/
- #ifdefZ_SOLO
- /*使用自定义的内存分配策略*/
- void*myallocOF((void*,unsigned,unsigned));
- voidmyfreeOF((void*,void*));
- void*myalloc(q,n,m)
- void*q;
- unsignedn,m;
- {
- q=Z_NULL;
- returncalloc(n,m);
- }
- voidmyfree(void*q,void*p)
- {
- q=Z_NULL;
- free(p);
- }
- staticalloc_funczalloc=myalloc;
- staticfree_funczfree=myfree;
- #else/*!Z_SOLO*/
- /*使用zlib默认的内存分配策略*/
- staticalloc_funczalloc=(alloc_func)0;
- staticfree_funczfree=(free_func)0;
/* example.c -- usage example of the zlib compression library * Copyright (C) 1995-2006, 2011 Jean-loup Gailly. * For conditions of distribution and use, see copyright notice in zlib.h */ /* @(#) $Id$ */ #include "zlib.h" #include <stdio.h> #ifdef STDC # include <string.h> # include <stdlib.h> #endif #if defined(VMS) || defined(RISCOS) # define TESTFILE "foo-gz" #else # define TESTFILE "foo.gz" #endif #define CHECK_ERR(err, msg) { \ if (err != Z_OK) { \ fprintf(stderr, "%s error: %d\n", msg, err); \ exit(1); \ } \ } const char hello[] = "hello, hello!"; /* 字符长度为14(末尾还有一个null字符) */ /* "hello world" would be more standard, but the repeated "hello" * stresses the compression code better, sorry... */ const char dictionary[] = "hello"; uLong dictId; /* 字典的Adler32校验值 */ void test_deflate OF((Byte *compr, uLong comprLen)); void test_inflate OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); void test_large_deflate OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); void test_large_inflate OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); void test_flush OF((Byte *compr, uLong *comprLen)); void test_sync OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); void test_dict_deflate OF((Byte *compr, uLong comprLen)); void test_dict_inflate OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); int main OF((int argc, char *argv[])); /* Z_SOLO表示把zlib库编译成单独的不依赖第三方的库 */ #ifdef Z_SOLO /* 使用自定义的内存分配策略 */ void *myalloc OF((void *, unsigned, unsigned)); void myfree OF((void *, void *)); void *myalloc(q, n, m) void *q; unsigned n, m; { q = Z_NULL; return calloc(n, m); } void myfree(void *q, void *p) { q = Z_NULL; free(p); } static alloc_func zalloc = myalloc; static free_func zfree = myfree; #else /* !Z_SOLO */ /* 使用zlib默认的内存分配策略 */ static alloc_func zalloc = (alloc_func)0; static free_func zfree = (free_func)0;下面测试compress和uncompress的用法:
- voidtest_compressOF((Byte*compr,uLongcomprLen,
- Byte*uncompr,uLonguncomprLen));
- voidtest_gzioOF((constchar*fname,
- Byte*uncompr,uLonguncomprLen));
- /*===========================================================================
- *测试compress()和uncompress()
- */
- voidtest_compress(compr,comprLen,uncompr,uncomprLen)
- Byte*compr,*uncompr;
- uLongcomprLen,uncomprLen;
- {
- interr;
- uLonglen=(uLong)strlen(hello)+1;/*获取字符串长度*/
- /*压缩字符串*/
- err=compress(compr,&comprLen,(constBytef*)hello,len);
- CHECK_ERR(err,"compress");
- strcpy((char*)uncompr,"garbage");
- /*解压字符串*/
- err=uncompress(uncompr,&uncomprLen,compr,comprLen);
- CHECK_ERR(err,"uncompress");
- /*比较解压后的结果*/
- if(strcmp((char*)uncompr,hello)){
- fprintf(stderr,"baduncompress\n");
- exit(1);
- }else{
- printf("uncompress():%s\n",(char*)uncompr);
- }
- }
void test_compress OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); void test_gzio OF((const char *fname, Byte *uncompr, uLong uncomprLen)); /* =========================================================================== * 测试compress()和uncompress() */ void test_compress(compr, comprLen, uncompr, uncomprLen) Byte *compr, *uncompr; uLong comprLen, uncomprLen; { int err; uLong len = (uLong)strlen(hello)+1; /* 获取字符串长度 */ /* 压缩字符串 */ err = compress(compr, &comprLen, (const Bytef*)hello, len); CHECK_ERR(err, "compress"); strcpy((char*)uncompr, "garbage"); /* 解压字符串 */ err = uncompress(uncompr, &uncomprLen, compr, comprLen); CHECK_ERR(err, "uncompress"); /* 比较解压后的结果 */ if (strcmp((char*)uncompr, hello)) { fprintf(stderr, "bad uncompress\n"); exit(1); } else { printf("uncompress(): %s\n", (char *)uncompr); } }下面测试gzip文件的读写操作:
- /*===========================================================================
- *测试.gz文件的读写操作
- */
- voidtest_gzio(fname,uncompr,uncomprLen)
- constchar*fname;/*gz文件名*/
- Byte*uncompr;
- uLonguncomprLen;
- {
- #ifdefNO_GZCOMPRESS
- fprintf(stderr,"NO_GZCOMPRESS--gz*functionscannotcompress\n");
- #else
- interr;
- intlen=(int)strlen(hello)+1;
- gzFilefile;
- z_off_tpos;
- file=gzopen(fname,"wb");/*打开要写入的gz文件*/
- if(file==NULL){
- fprintf(stderr,"gzopenerror\n");
- exit(1);
- }
- gzputc(file,'h');/*写入一个字符'h'*/
- if(gzputs(file,"ello")!=4){/*写入字符串"ello"*/
- fprintf(stderr,"gzputserr:%s\n",gzerror(file,&err));
- exit(1);
- }
- if(gzprintf(file,",%s!","hello")!=8){/*按格式写入字符串",hello!"*/
- fprintf(stderr,"gzprintferr:%s\n",gzerror(file,&err));
- exit(1);
- }
- gzseek(file,1L,SEEK_CUR);/*读写头向前移动1字节(即添加一个0字节)*/
- gzclose(file);/*关闭gz文件*/
- file=gzopen(fname,"rb");/*打开要读取的gz文件*/
- if(file==NULL){
- fprintf(stderr,"gzopenerror\n");
- exit(1);
- }
- strcpy((char*)uncompr,"garbage");
- /*从压缩文件中读取给定大小的解压字节数*/
- if(gzread(file,uncompr,(unsigned)uncomprLen)!=len){
- fprintf(stderr,"gzreaderr:%s\n",gzerror(file,&err));
- exit(1);
- }
- if(strcmp((char*)uncompr,hello)){/*比较解压后的结果*/
- fprintf(stderr,"badgzread:%s\n",(char*)uncompr);
- exit(1);
- }else{
- printf("gzread():%s\n",(char*)uncompr);
- }
- pos=gzseek(file,-8L,SEEK_CUR);/*读写头向后移动8字节,应该停留在第6个字符处*/
- if(pos!=6||gztell(file)!=pos){/*判断是否停留在第6个字符处*/
- fprintf(stderr,"gzseekerror,pos=%ld,gztell=%ld\n",
- (long)pos,(long)gztell(file));
- exit(1);
- }
- if(gzgetc(file)!=''){/*从当前位置读取1个字符,应该为字符''*/
- fprintf(stderr,"gzgetcerror\n");
- exit(1);
- }
- if(gzungetc('',file)!=''){/*推回这个字符到流中*/
- fprintf(stderr,"gzungetcerror\n");
- exit(1);
- }
- /*从压缩文件当前位置读取指定长度的解压字节数,直到len-1个字符被读取*/
- gzgets(file,(char*)uncompr,(int)uncomprLen);
- if(strlen((char*)uncompr)!=7){/*"hello!"*/
- fprintf(stderr,"gzgetserraftergzseek:%s\n",gzerror(file,&err));
- exit(1);
- }
- if(strcmp((char*)uncompr,hello+6)){
- fprintf(stderr,"badgzgetsaftergzseek\n");
- exit(1);
- }else{
- printf("gzgets()aftergzseek:%s\n",(char*)uncompr);
- }
- gzclose(file);/*关闭gz文件*/
- #endif
- }
- #endif/*Z_SOLO*/
/* =========================================================================== * 测试.gz文件的读写操作 */ void test_gzio(fname, uncompr, uncomprLen) const char *fname; /* gz文件名 */ Byte *uncompr; uLong uncomprLen; { #ifdef NO_GZCOMPRESS fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n"); #else int err; int len = (int)strlen(hello)+1; gzFile file; z_off_t pos; file = gzopen(fname, "wb"); /* 打开要写入的gz文件 */ if (file == NULL) { fprintf(stderr, "gzopen error\n"); exit(1); } gzputc(file, 'h'); /* 写入一个字符'h' */ if (gzputs(file, "ello") != 4) { /* 写入字符串"ello" */ fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err)); exit(1); } if (gzprintf(file, ", %s!", "hello") != 8) { /* 按格式写入字符串", hello!" */ fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err)); exit(1); } gzseek(file, 1L, SEEK_CUR); /* 读写头向前移动1字节(即添加一个0字节) */ gzclose(file); /* 关闭gz文件 */ file = gzopen(fname, "rb"); /* 打开要读取的gz文件 */ if (file == NULL) { fprintf(stderr, "gzopen error\n"); exit(1); } strcpy((char*)uncompr, "garbage"); /* 从压缩文件中读取给定大小的解压字节数 */ if (gzread(file, uncompr, (unsigned)uncomprLen) != len) { fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); exit(1); } if (strcmp((char*)uncompr, hello)) { /* 比较解压后的结果 */ fprintf(stderr, "bad gzread: %s\n", (char*)uncompr); exit(1); } else { printf("gzread(): %s\n", (char*)uncompr); } pos = gzseek(file, -8L, SEEK_CUR); /* 读写头向后移动8字节,应该停留在第6个字符处 */ if (pos != 6 || gztell(file) != pos) { /* 判断是否停留在第6个字符处 */ fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n", (long)pos, (long)gztell(file)); exit(1); } if (gzgetc(file) != ' ') { /* 从当前位置读取1个字符,应该为字符' ' */ fprintf(stderr, "gzgetc error\n"); exit(1); } if (gzungetc(' ', file) != ' ') { /* 推回这个字符到流中 */ fprintf(stderr, "gzungetc error\n"); exit(1); } /* 从压缩文件当前位置读取指定长度的解压字节数,直到len-1个字符被读取 */ gzgets(file, (char*)uncompr, (int)uncomprLen); if (strlen((char*)uncompr) != 7) { /* " hello!" */ fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err)); exit(1); } if (strcmp((char*)uncompr, hello + 6)) { fprintf(stderr, "bad gzgets after gzseek\n"); exit(1); } else { printf("gzgets() after gzseek: %s\n", (char*)uncompr); } gzclose(file); /* 关闭gz文件 */ #endif } #endif /* Z_SOLO */下面用小缓冲区测试压缩、解压操作(deflate/deflate):
- /*===========================================================================
- *测试deflate():使用小缓冲区
- */
- voidtest_deflate(compr,comprLen)
- Byte*compr;
- uLongcomprLen;
- {
- z_streamc_stream;/*压缩流*/
- interr;
- uLonglen=(uLong)strlen(hello)+1;
- /*这三个字段要在defalteInit之前初始化*/
- c_stream.zalloc=zalloc;
- c_stream.zfree=zfree;
- c_stream.opaque=(voidpf)0;
- /*初始化压缩流的状态,使用默认压缩级别*/
- err=deflateInit(&c_stream,Z_DEFAULT_COMPRESSION);
- CHECK_ERR(err,"deflateInit");
- /*设置压缩操作的输入数据和输出缓冲区*/
- c_stream.next_in=(Bytef*)hello;/*输入缓冲区指向输入字符串*/
- c_stream.next_out=compr;
- /*第一个循环:将flush设为Z_NO_FLUSH(表示还有输入数据未读完),将所有输入都读进去并进行压缩
- 根据avail_in和avail_out,不停地调用deflate将输入缓冲区的数据压缩
- 并写到输出缓冲区,直到输入字符串读完或输出缓冲区用完
- */
- while(c_stream.total_in!=len&&c_stream.total_out<comprLen){
- c_stream.avail_in=c_stream.avail_out=1;/*强制小缓冲区*/
- err=deflate(&c_stream,Z_NO_FLUSH);
- CHECK_ERR(err,"deflate");
- }
- /*第二个循环:将flush设置为Z_FINISH,不再输入,让deflate()完成全部的压缩输出
- 注意因为deflate压缩时可能是异步的(为了加速压缩,读取一次输入后不一定立刻就会产生压缩输出,
- 可能读完K字节后才会产生输出),所以上一个循环可能还没产生全部输出,需要这个循环,让flush保持Z_FINISH
- (表示输入数据已读完),多次调用deflate(),直到返回Z_STREAM_END,表示处理完全部输入并产生了全部的压缩输出
- */
- for(;;){/*完成压缩流的刷新,仍然强制小缓冲区*/
- c_stream.avail_out=1;
- err=deflate(&c_stream,Z_FINISH);
- if(err==Z_STREAM_END)break;
- CHECK_ERR(err,"deflate");
- }
- err=deflateEnd(&c_stream);/*释放压缩流的资源*/
- CHECK_ERR(err,"deflateEnd");
- }
- /*===========================================================================
- *测试inflate():使用小缓冲区
- */
- voidtest_inflate(compr,comprLen,uncompr,uncomprLen)
- Byte*compr,*uncompr;
- uLongcomprLen,uncomprLen;
- {
- interr;
- z_streamd_stream;/*解压流*/
- strcpy((char*)uncompr,"garbage");
- /*这些个字段要在infalteInit之前初始化*/
- d_stream.zalloc=zalloc;
- d_stream.zfree=zfree;
- d_stream.opaque=(voidpf)0;
- d_stream.next_in=compr;/*设置输入缓冲区*/
- d_stream.avail_in=0;
- d_stream.next_out=uncompr;/*设置输出缓冲区*/
- /*初始化解压流的状态*/
- err=inflateInit(&d_stream);
- CHECK_ERR(err,"inflateInit");
- /*只需一个循环:根据avail_in和avail_out,不停地调用inflate将输入缓冲区的数据
- 解压,直到返回Z_STREAM_END,表示处理完全部输入并产生了全部的解压输出
- 这里与flush参数是否为Z_FINISH无关
- */
- while(d_stream.total_out<uncomprLen&&d_stream.total_in<comprLen){
- d_stream.avail_in=d_stream.avail_out=1;/*强制小缓冲区*/
- err=inflate(&d_stream,Z_NO_FLUSH);
- if(err==Z_STREAM_END)break;
- CHECK_ERR(err,"inflate");
- }
- err=inflateEnd(&d_stream);/*释放解压流的资源*/
- CHECK_ERR(err,"inflateEnd");
- if(strcmp((char*)uncompr,hello)){/*比较解压后的数据*/
- fprintf(stderr,"badinflate\n");
- exit(1);
- }else{
- printf("inflate():%s\n",(char*)uncompr);
- }
- }
/* =========================================================================== * 测试deflate():使用小缓冲区 */ void test_deflate(compr, comprLen) Byte *compr; uLong comprLen; { z_stream c_stream; /* 压缩流 */ int err; uLong len = (uLong)strlen(hello)+1; /* 这三个字段要在defalteInit之前初始化 */ c_stream.zalloc = zalloc; c_stream.zfree = zfree; c_stream.opaque = (voidpf)0; /* 初始化压缩流的状态,使用默认压缩级别 */ err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); CHECK_ERR(err, "deflateInit"); /* 设置压缩操作的输入数据和输出缓冲区 */ c_stream.next_in = (Bytef*)hello; /* 输入缓冲区指向输入字符串 */ c_stream.next_out = compr; /* 第一个循环:将flush设为Z_NO_FLUSH(表示还有输入数据未读完),将所有输入都读进去并进行压缩 根据avail_in和avail_out,不停地调用deflate将输入缓冲区的数据压缩 并写到输出缓冲区,直到输入字符串读完或输出缓冲区用完 */ while (c_stream.total_in != len && c_stream.total_out < comprLen) { c_stream.avail_in = c_stream.avail_out = 1; /* 强制小缓冲区 */ err = deflate(&c_stream, Z_NO_FLUSH); CHECK_ERR(err, "deflate"); } /* 第二个循环:将flush设置为Z_FINISH,不再输入,让deflate()完成全部的压缩输出 注意因为deflate压缩时可能是异步的(为了加速压缩,读取一次输入后不一定立刻就会产生压缩输出, 可能读完K字节后才会产生输出),所以上一个循环可能还没产生全部输出,需要这个循环,让flush保持Z_FINISH (表示输入数据已读完),多次调用deflate(),直到返回Z_STREAM_END,表示处理完全部输入并产生了全部的压缩输出 */ for (;;) { /* 完成压缩流的刷新,仍然强制小缓冲区 */ c_stream.avail_out = 1; err = deflate(&c_stream, Z_FINISH); if (err == Z_STREAM_END) break; CHECK_ERR(err, "deflate"); } err = deflateEnd(&c_stream); /* 释放压缩流的资源 */ CHECK_ERR(err, "deflateEnd"); } /* =========================================================================== * 测试inflate():使用小缓冲区 */ void test_inflate(compr, comprLen, uncompr, uncomprLen) Byte *compr, *uncompr; uLong comprLen, uncomprLen; { int err; z_stream d_stream; /* 解压流 */ strcpy((char*)uncompr, "garbage"); /* 这些个字段要在infalteInit之前初始化 */ d_stream.zalloc = zalloc; d_stream.zfree = zfree; d_stream.opaque = (voidpf)0; d_stream.next_in = compr; /* 设置输入缓冲区 */ d_stream.avail_in = 0; d_stream.next_out = uncompr; /* 设置输出缓冲区 */ /* 初始化解压流的状态 */ err = inflateInit(&d_stream); CHECK_ERR(err, "inflateInit"); /* 只需一个循环:根据avail_in和avail_out,不停地调用inflate将输入缓冲区的数据 解压,直到返回Z_STREAM_END,表示处理完全部输入并产生了全部的解压输出 这里与flush参数是否为Z_FINISH无关 */ while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { d_stream.avail_in = d_stream.avail_out = 1; /* 强制小缓冲区 */ err = inflate(&d_stream, Z_NO_FLUSH); if (err == Z_STREAM_END) break; CHECK_ERR(err, "inflate"); } err = inflateEnd(&d_stream); /* 释放解压流的资源 */ CHECK_ERR(err, "inflateEnd"); if (strcmp((char*)uncompr, hello)) { /* 比较解压后的数据 */ fprintf(stderr, "bad inflate\n"); exit(1); } else { printf("inflate(): %s\n", (char *)uncompr); } }下面使用大缓冲区测试压缩、解压操作(deflate/deflate):
- /*===========================================================================
- *测试deflate():使用大缓冲区和动态改变的压缩级别
- */
- voidtest_large_deflate(compr,comprLen,uncompr,uncomprLen)
- Byte*compr,*uncompr;
- uLongcomprLen,uncomprLen;
- {
- z_streamc_stream;/*压缩流*/
- interr;
- /*这三个字段要在defalteInit之前初始化*/
- c_stream.zalloc=zalloc;
- c_stream.zfree=zfree;
- c_stream.opaque=(voidpf)0;
- /*初始化压缩流的状态,使用最快速度压缩*/
- err=deflateInit(&c_stream,Z_BEST_SPEED);
- CHECK_ERR(err,"deflateInit");
- c_stream.next_out=compr;
- c_stream.avail_out=(uInt)comprLen;
- /*这里,uncompr几乎都为0,因此可以很好地被压缩*/
- c_stream.next_in=uncompr;
- c_stream.avail_in=(uInt)uncomprLen;
- err=deflate(&c_stream,Z_NO_FLUSH);/*压缩输入数据*/
- CHECK_ERR(err,"deflate");
- if(c_stream.avail_in!=0){
- fprintf(stderr,"deflatenotgreedy\n");
- exit(1);
- }
- /*把已压缩的数据转换成未压缩:*/
- /*设置流的压缩级别(为未压缩)和压缩策略*/
- deflateParams(&c_stream,Z_NO_COMPRESSION,Z_DEFAULT_STRATEGY);
- c_stream.next_in=compr;
- c_stream.avail_in=(uInt)comprLen/2;
- err=deflate(&c_stream,Z_NO_FLUSH);
- CHECK_ERR(err,"deflate");
- /*转换回压缩模式(最高压缩率):*/
- deflateParams(&c_stream,Z_BEST_COMPRESSION,Z_FILTERED);
- c_stream.next_in=uncompr;
- c_stream.avail_in=(uInt)uncomprLen;
- err=deflate(&c_stream,Z_NO_FLUSH);
- CHECK_ERR(err,"deflate");
- /*流刷新,产生全部压缩输出*/
- err=deflate(&c_stream,Z_FINISH);
- if(err!=Z_STREAM_END){
- fprintf(stderr,"deflateshouldreportZ_STREAM_END\n");
- exit(1);
- }
- err=deflateEnd(&c_stream);/*释放流的资源*/
- CHECK_ERR(err,"deflateEnd");
- }
- /*===========================================================================
- *测试inflate():使用大缓冲区
- */
- voidtest_large_inflate(compr,comprLen,uncompr,uncomprLen)
- Byte*compr,*uncompr;
- uLongcomprLen,uncomprLen;
- {
- interr;
- z_streamd_stream;/*解压流*/
- strcpy((char*)uncompr,"garbage");
- /*这些个字段要在infalteInit之前初始化*/
- d_stream.zalloc=zalloc;
- d_stream.zfree=zfree;
- d_stream.opaque=(voidpf)0;
- d_stream.next_in=compr;
- d_stream.avail_in=(uInt)comprLen;
- /*初始化解压流*/
- err=inflateInit(&d_stream);
- CHECK_ERR(err,"inflateInit");
- /*解压*/
- for(;;){
- d_stream.next_out=uncompr;/*抛弃输出*/
- d_stream.avail_out=(uInt)uncomprLen;
- err=inflate(&d_stream,Z_NO_FLUSH);/*解压输入数据*/
- if(err==Z_STREAM_END)break;
- CHECK_ERR(err,"largeinflate");
- }
- err=inflateEnd(&d_stream);
- CHECK_ERR(err,"inflateEnd");
- if(d_stream.total_out!=2*uncomprLen+comprLen/2){
- fprintf(stderr,"badlargeinflate:%ld\n",d_stream.total_out);
- exit(1);
- }else{
- printf("large_inflate():OK\n");
- }
- }
/* =========================================================================== * 测试deflate():使用大缓冲区和动态改变的压缩级别 */ void test_large_deflate(compr, comprLen, uncompr, uncomprLen) Byte *compr, *uncompr; uLong comprLen, uncomprLen; { z_stream c_stream; /* 压缩流 */ int err; /* 这三个字段要在defalteInit之前初始化 */ c_stream.zalloc = zalloc; c_stream.zfree = zfree; c_stream.opaque = (voidpf)0; /* 初始化压缩流的状态,使用最快速度压缩 */ err = deflateInit(&c_stream, Z_BEST_SPEED); CHECK_ERR(err, "deflateInit"); c_stream.next_out = compr; c_stream.avail_out = (uInt)comprLen; /* 这里,uncompr几乎都为0,因此可以很好地被压缩 */ c_stream.next_in = uncompr; c_stream.avail_in = (uInt)uncomprLen; err = deflate(&c_stream, Z_NO_FLUSH); /* 压缩输入数据 */ CHECK_ERR(err, "deflate"); if (c_stream.avail_in != 0) { fprintf(stderr, "deflate not greedy\n"); exit(1); } /* 把已压缩的数据转换成未压缩: */ /* 设置流的压缩级别(为未压缩)和压缩策略 */ deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY); c_stream.next_in = compr; c_stream.avail_in = (uInt)comprLen/2; err = deflate(&c_stream, Z_NO_FLUSH); CHECK_ERR(err, "deflate"); /* 转换回压缩模式(最高压缩率): */ deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED); c_stream.next_in = uncompr; c_stream.avail_in = (uInt)uncomprLen; err = deflate(&c_stream, Z_NO_FLUSH); CHECK_ERR(err, "deflate"); /* 流刷新,产生全部压缩输出 */ err = deflate(&c_stream, Z_FINISH); if (err != Z_STREAM_END) { fprintf(stderr, "deflate should report Z_STREAM_END\n"); exit(1); } err = deflateEnd(&c_stream); /* 释放流的资源 */ CHECK_ERR(err, "deflateEnd"); } /* =========================================================================== * 测试inflate():使用大缓冲区 */ void test_large_inflate(compr, comprLen, uncompr, uncomprLen) Byte *compr, *uncompr; uLong comprLen, uncomprLen; { int err; z_stream d_stream; /* 解压流 */ strcpy((char*)uncompr, "garbage"); /* 这些个字段要在infalteInit之前初始化 */ d_stream.zalloc = zalloc; d_stream.zfree = zfree; d_stream.opaque = (voidpf)0; d_stream.next_in = compr; d_stream.avail_in = (uInt)comprLen; /* 初始化解压流 */ err = inflateInit(&d_stream); CHECK_ERR(err, "inflateInit"); /* 解压 */ for (;;) { d_stream.next_out = uncompr; /* 抛弃输出 */ d_stream.avail_out = (uInt)uncomprLen; err = inflate(&d_stream, Z_NO_FLUSH); /* 解压输入数据 */ if (err == Z_STREAM_END) break; CHECK_ERR(err, "large inflate"); } err = inflateEnd(&d_stream); CHECK_ERR(err, "inflateEnd"); if (d_stream.total_out != 2*uncomprLen + comprLen/2) { fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out); exit(1); } else { printf("large_inflate(): OK\n"); } }下面使用完全刷新模式测试压缩操作deflate:
- /*===========================================================================
- *测试deflate():使用完全刷新
- */
- voidtest_flush(compr,comprLen)
- Byte*compr;
- uLong*comprLen;
- {
- z_streamc_stream;/*压缩流*/
- interr;
- uIntlen=(uInt)strlen(hello)+1;
- c_stream.zalloc=zalloc;
- c_stream.zfree=zfree;
- c_stream.opaque=(voidpf)0;
- err=deflateInit(&c_stream,Z_DEFAULT_COMPRESSION);
- CHECK_ERR(err,"deflateInit");
- c_stream.next_in=(Bytef*)hello;
- c_stream.next_out=compr;
- c_stream.avail_in=3;
- c_stream.avail_out=(uInt)*comprLen;
- /*使用完全刷新来压缩字符串*/
- err=deflate(&c_stream,Z_FULL_FLUSH);
- CHECK_ERR(err,"deflate");
- compr[3]++;/*在第一个压缩块中强制产生一个错误*/
- c_stream.avail_in=len-3;
- err=deflate(&c_stream,Z_FINISH);
- if(err!=Z_STREAM_END){
- CHECK_ERR(err,"deflate");
- }
- err=deflateEnd(&c_stream);
- CHECK_ERR(err,"deflateEnd");
- *comprLen=c_stream.total_out;
- }
/* =========================================================================== * 测试deflate():使用完全刷新 */ void test_flush(compr, comprLen) Byte *compr; uLong *comprLen; { z_stream c_stream; /* 压缩流 */ int err; uInt len = (uInt)strlen(hello)+1; c_stream.zalloc = zalloc; c_stream.zfree = zfree; c_stream.opaque = (voidpf)0; err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); CHECK_ERR(err, "deflateInit"); c_stream.next_in = (Bytef*)hello; c_stream.next_out = compr; c_stream.avail_in = 3; c_stream.avail_out = (uInt)*comprLen; /* 使用完全刷新来压缩字符串 */ err = deflate(&c_stream, Z_FULL_FLUSH); CHECK_ERR(err, "deflate"); compr[3]++; /* 在第一个压缩块中强制产生一个错误 */ c_stream.avail_in = len - 3; err = deflate(&c_stream, Z_FINISH); if (err != Z_STREAM_END) { CHECK_ERR(err, "deflate"); } err = deflateEnd(&c_stream); CHECK_ERR(err, "deflateEnd"); *comprLen = c_stream.total_out; }下面测试同步方式的解压操作inflateSync:
- /*===========================================================================
- *TestinflateSync()
- */
- voidtest_sync(compr,comprLen,uncompr,uncomprLen)
- Byte*compr,*uncompr;
- uLongcomprLen,uncomprLen;
- {
- interr;
- z_streamd_stream;/*解压流*/
- strcpy((char*)uncompr,"garbage");
- d_stream.zalloc=zalloc;
- d_stream.zfree=zfree;
- d_stream.opaque=(voidpf)0;
- d_stream.next_in=compr;/*设置输入缓冲区*/
- d_stream.avail_in=2;/*只读取zlib头部信息*/
- err=inflateInit(&d_stream);
- CHECK_ERR(err,"inflateInit");
- d_stream.next_out=uncompr;/*设置输出缓冲区*/
- d_stream.avail_out=(uInt)uncomprLen;
- inflate(&d_stream,Z_NO_FLUSH);
- CHECK_ERR(err,"inflate");
- d_stream.avail_in=(uInt)comprLen-2;/*读取所有压缩数据*/
- err=inflateSync(&d_stream);/*但忽略损坏的部分*/
- CHECK_ERR(err,"inflateSync");
- err=inflate(&d_stream,Z_FINISH);/*完成解压*/
- if(err!=Z_DATA_ERROR){
- fprintf(stderr,"inflateshouldreportDATA_ERROR\n");
- /*因为不正确的adler32*/
- exit(1);
- }
- err=inflateEnd(&d_stream);
- CHECK_ERR(err,"inflateEnd");
- printf("afterinflateSync():hel%s\n",(char*)uncompr);
- }
/* =========================================================================== * Test inflateSync() */ void test_sync(compr, comprLen, uncompr, uncomprLen) Byte *compr, *uncompr; uLong comprLen, uncomprLen; { int err; z_stream d_stream; /* 解压流 */ strcpy((char*)uncompr, "garbage"); d_stream.zalloc = zalloc; d_stream.zfree = zfree; d_stream.opaque = (voidpf)0; d_stream.next_in = compr; /* 设置输入缓冲区 */ d_stream.avail_in = 2; /* 只读取zlib头部信息 */ err = inflateInit(&d_stream); CHECK_ERR(err, "inflateInit"); d_stream.next_out = uncompr; /* 设置输出缓冲区 */ d_stream.avail_out = (uInt)uncomprLen; inflate(&d_stream, Z_NO_FLUSH); CHECK_ERR(err, "inflate"); d_stream.avail_in = (uInt)comprLen-2; /* 读取所有压缩数据 */ err = inflateSync(&d_stream); /* 但忽略损坏的部分 */ CHECK_ERR(err, "inflateSync"); err = inflate(&d_stream, Z_FINISH); /* 完成解压 */ if (err != Z_DATA_ERROR) { fprintf(stderr, "inflate should report DATA_ERROR\n"); /* 因为不正确的adler32 */ exit(1); } err = inflateEnd(&d_stream); CHECK_ERR(err, "inflateEnd"); printf("after inflateSync(): hel%s\n", (char *)uncompr); }下面用预设的字典测试压缩、解压操作(deflate/inflate):
- /*===========================================================================
- *测试:deflate():使用预设的字典
- */
- voidtest_dict_deflate(compr,comprLen)
- Byte*compr;
- uLongcomprLen;
- {
- z_streamc_stream;/*压缩流*/
- interr;
- c_stream.zalloc=zalloc;
- c_stream.zfree=zfree;
- c_stream.opaque=(voidpf)0;
- err=deflateInit(&c_stream,Z_BEST_COMPRESSION);
- CHECK_ERR(err,"deflateInit");
- /*设置压缩流要使用的字典*/
- err=deflateSetDictionary(&c_stream,
- (constBytef*)dictionary,(int)sizeof(dictionary));
- CHECK_ERR(err,"deflateSetDictionary");
- dictId=c_stream.adler;/*得到字典的Alder32校验值*/
- c_stream.next_out=compr;
- c_stream.avail_out=(uInt)comprLen;
- c_stream.next_in=(Bytef*)hello;/*输入要压缩的字符串*/
- c_stream.avail_in=(uInt)strlen(hello)+1;
- /*直接进行压缩*/
- err=deflate(&c_stream,Z_FINISH);
- if(err!=Z_STREAM_END){
- fprintf(stderr,"deflateshouldreportZ_STREAM_END\n");
- exit(1);
- }
- err=deflateEnd(&c_stream);
- CHECK_ERR(err,"deflateEnd");
- }
- /*===========================================================================
- *测试inflate():使用预设的字典
- */
- voidtest_dict_inflate(compr,comprLen,uncompr,uncomprLen)
- Byte*compr,*uncompr;
- uLongcomprLen,uncomprLen;
- {
- interr;
- z_streamd_stream;/*解压流*/
- strcpy((char*)uncompr,"garbage");
- d_stream.zalloc=zalloc;
- d_stream.zfree=zfree;
- d_stream.opaque=(voidpf)0;
- d_stream.next_in=compr;
- d_stream.avail_in=(uInt)comprLen;
- err=inflateInit(&d_stream);
- CHECK_ERR(err,"inflateInit");
- d_stream.next_out=uncompr;
- d_stream.avail_out=(uInt)uncomprLen;
- for(;;){/*解压*/
- err=inflate(&d_stream,Z_NO_FLUSH);
- if(err==Z_STREAM_END)break;
- if(err==Z_NEED_DICT){/*如果需要字典*/
- if(d_stream.adler!=dictId){/*校验是否与压缩时的字典值一致*/
- fprintf(stderr,"unexpecteddictionary");
- exit(1);
- }
- /*设置解压需要的字典*/
- err=inflateSetDictionary(&d_stream,(constBytef*)dictionary,
- (int)sizeof(dictionary));
- }
- CHECK_ERR(err,"inflatewithdict");
- }
- err=inflateEnd(&d_stream);
- CHECK_ERR(err,"inflateEnd");
- if(strcmp((char*)uncompr,hello)){/*比较解压后的字符串*/
- fprintf(stderr,"badinflatewithdict\n");
- exit(1);
- }else{
- printf("inflatewithdictionary:%s\n",(char*)uncompr);
- }
- }
/* =========================================================================== * 测试:deflate():使用预设的字典 */ void test_dict_deflate(compr, comprLen) Byte *compr; uLong comprLen; { z_stream c_stream; /* 压缩流 */ int err; c_stream.zalloc = zalloc; c_stream.zfree = zfree; c_stream.opaque = (voidpf)0; err = deflateInit(&c_stream, Z_BEST_COMPRESSION); CHECK_ERR(err, "deflateInit"); /* 设置压缩流要使用的字典 */ err = deflateSetDictionary(&c_stream, (const Bytef*)dictionary, (int)sizeof(dictionary)); CHECK_ERR(err, "deflateSetDictionary"); dictId = c_stream.adler; /* 得到字典的Alder32校验值 */ c_stream.next_out = compr; c_stream.avail_out = (uInt)comprLen; c_stream.next_in = (Bytef*)hello; /* 输入要压缩的字符串 */ c_stream.avail_in = (uInt)strlen(hello)+1; /* 直接进行压缩 */ err = deflate(&c_stream, Z_FINISH); if (err != Z_STREAM_END) { fprintf(stderr, "deflate should report Z_STREAM_END\n"); exit(1); } err = deflateEnd(&c_stream); CHECK_ERR(err, "deflateEnd"); } /* =========================================================================== * 测试inflate():使用预设的字典 */ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen) Byte *compr, *uncompr; uLong comprLen, uncomprLen; { int err; z_stream d_stream; /* 解压流 */ strcpy((char*)uncompr, "garbage"); d_stream.zalloc = zalloc; d_stream.zfree = zfree; d_stream.opaque = (voidpf)0; d_stream.next_in = compr; d_stream.avail_in = (uInt)comprLen; err = inflateInit(&d_stream); CHECK_ERR(err, "inflateInit"); d_stream.next_out = uncompr; d_stream.avail_out = (uInt)uncomprLen; for (;;) { /* 解压 */ err = inflate(&d_stream, Z_NO_FLUSH); if (err == Z_STREAM_END) break; if (err == Z_NEED_DICT) { /* 如果需要字典 */ if (d_stream.adler != dictId) { /* 校验是否与压缩时的字典值一致 */ fprintf(stderr, "unexpected dictionary"); exit(1); } /* 设置解压需要的字典 */ err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary, (int)sizeof(dictionary)); } CHECK_ERR(err, "inflate with dict"); } err = inflateEnd(&d_stream); CHECK_ERR(err, "inflateEnd"); if (strcmp((char*)uncompr, hello)) { /* 比较解压后的字符串 */ fprintf(stderr, "bad inflate with dict\n"); exit(1); } else { printf("inflate with dictionary: %s\n", (char *)uncompr); } }下面是命令行程序:
- /*===========================================================================
- *Usage:example[output.gz[input.gz]]
- */
- intmain(argc,argv)
- intargc;
- char*argv[];
- {
- Byte*compr,*uncompr;
- uLongcomprLen=10000*sizeof(int);/*在MSDOS上不会溢出*/
- uLonguncomprLen=comprLen;
- staticconstchar*myVersion=ZLIB_VERSION;
- /*检查zlib版本是否一致*/
- if(zlibVersion()[0]!=myVersion[0]){
- fprintf(stderr,"incompatiblezlibversion\n");
- exit(1);
- }elseif(strcmp(zlibVersion(),ZLIB_VERSION)!=0){
- fprintf(stderr,"warning:differentzlibversion\n");
- }
- /*打印版本和zlib编译信息*/
- printf("zlibversion%s=0x%04x,compileflags=0x%lx\n",
- ZLIB_VERSION,ZLIB_VERNUM,zlibCompileFlags());
- /*分配输入、输出缓冲区的内存*/
- compr=(Byte*)calloc((uInt)comprLen,1);
- uncompr=(Byte*)calloc((uInt)uncomprLen,1);
- /*清空compr和uncompr,以避免读到未初始化的数据,并且确保uncompr能很好
- *地被压缩
- */
- if(compr==Z_NULL||uncompr==Z_NULL){
- printf("outofmemory\n");
- exit(1);
- }
- /*下面运行各个测试函数*/
- #ifdefZ_SOLO
- argc=strlen(argv[0]);
- #else
- test_compress(compr,comprLen,uncompr,uncomprLen);
- test_gzio((argc>1?argv[1]:TESTFILE),
- uncompr,uncomprLen);
- #endif
- test_deflate(compr,comprLen);
- test_inflate(compr,comprLen,uncompr,uncomprLen);
- test_large_deflate(compr,comprLen,uncompr,uncomprLen);
- test_large_inflate(compr,comprLen,uncompr,uncomprLen);
- test_flush(compr,&comprLen);
- test_sync(compr,comprLen,uncompr,uncomprLen);
- comprLen=uncomprLen;
- test_dict_deflate(compr,comprLen);
- test_dict_inflate(compr,comprLen,uncompr,uncomprLen);
- /*释放缓冲区资源*/
- free(compr);
- free(uncompr);
- return0;
- }
/* =========================================================================== * Usage: example [output.gz [input.gz]] */ int main(argc, argv) int argc; char *argv[]; { Byte *compr, *uncompr; uLong comprLen = 10000*sizeof(int); /* 在MSDOS上不会溢出 */ uLong uncomprLen = comprLen; static const char* myVersion = ZLIB_VERSION; /* 检查zlib版本是否一致 */ if (zlibVersion()[0] != myVersion[0]) { fprintf(stderr, "incompatible zlib version\n"); exit(1); } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) { fprintf(stderr, "warning: different zlib version\n"); } /* 打印版本和zlib编译信息 */ printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n", ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags()); /* 分配输入、输出缓冲区的内存 */ compr = (Byte*)calloc((uInt)comprLen, 1); uncompr = (Byte*)calloc((uInt)uncomprLen, 1); /* 清空compr和uncompr,以避免读到未初始化的数据,并且确保uncompr能很好 * 地被压缩 */ if (compr == Z_NULL || uncompr == Z_NULL) { printf("out of memory\n"); exit(1); } /* 下面运行各个测试函数 */ #ifdef Z_SOLO argc = strlen(argv[0]); #else test_compress(compr, comprLen, uncompr, uncomprLen); test_gzio((argc > 1 ? argv[1] : TESTFILE), uncompr, uncomprLen); #endif test_deflate(compr, comprLen); test_inflate(compr, comprLen, uncompr, uncomprLen); test_large_deflate(compr, comprLen, uncompr, uncomprLen); test_large_inflate(compr, comprLen, uncompr, uncomprLen); test_flush(compr, &comprLen); test_sync(compr, comprLen, uncompr, uncomprLen); comprLen = uncomprLen; test_dict_deflate(compr, comprLen); test_dict_inflate(compr, comprLen, uncompr, uncomprLen); /* 释放缓冲区资源 */ free(compr); free(uncompr); return 0; }
下面代码定义要压缩的字符串、压缩时使用的字典、压缩/解压缩的内存分配策略等。
- /*example.c--usageexampleofthezlibcompressionlibrary
- *Copyright(C)1995-2006,2011Jean-loupGailly.
- *Forconditionsofdistributionanduse,seecopyrightnoticeinzlib.h
- */
- /*@(#)$Id$*/
- #include"zlib.h"
- #include<stdio.h>
- #ifdefSTDC
- #include<string.h>
- #include<stdlib.h>
- #endif
- #ifdefined(VMS)||defined(RISCOS)
- #defineTESTFILE"foo-gz"
- #else
- #defineTESTFILE"foo.gz"
- #endif
- #defineCHECK_ERR(err,msg){\
- if(err!=Z_OK){\
- fprintf(stderr,"%serror:%d\n",msg,err);\
- exit(1);\
- }\
- }
- constcharhello[]="hello,hello!";/*字符长度为14(末尾还有一个null字符)*/
- /*"helloworld"wouldbemorestandard,buttherepeated"hello"
- *stressesthecompressioncodebetter,sorry...
- */
- constchardictionary[]="hello";
- uLongdictId;/*字典的Adler32校验值*/
- voidtest_deflateOF((Byte*compr,uLongcomprLen));
- voidtest_inflateOF((Byte*compr,uLongcomprLen,
- Byte*uncompr,uLonguncomprLen));
- voidtest_large_deflateOF((Byte*compr,uLongcomprLen,
- Byte*uncompr,uLonguncomprLen));
- voidtest_large_inflateOF((Byte*compr,uLongcomprLen,
- Byte*uncompr,uLonguncomprLen));
- voidtest_flushOF((Byte*compr,uLong*comprLen));
- voidtest_syncOF((Byte*compr,uLongcomprLen,
- Byte*uncompr,uLonguncomprLen));
- voidtest_dict_deflateOF((Byte*compr,uLongcomprLen));
- voidtest_dict_inflateOF((Byte*compr,uLongcomprLen,
- Byte*uncompr,uLonguncomprLen));
- intmainOF((intargc,char*argv[]));
- /*Z_SOLO表示把zlib库编译成单独的不依赖第三方的库*/
- #ifdefZ_SOLO
- /*使用自定义的内存分配策略*/
- void*myallocOF((void*,unsigned,unsigned));
- voidmyfreeOF((void*,void*));
- void*myalloc(q,n,m)
- void*q;
- unsignedn,m;
- {
- q=Z_NULL;
- returncalloc(n,m);
- }
- voidmyfree(void*q,void*p)
- {
- q=Z_NULL;
- free(p);
- }
- staticalloc_funczalloc=myalloc;
- staticfree_funczfree=myfree;
- #else/*!Z_SOLO*/
- /*使用zlib默认的内存分配策略*/
- staticalloc_funczalloc=(alloc_func)0;
- staticfree_funczfree=(free_func)0;
/* example.c -- usage example of the zlib compression library * Copyright (C) 1995-2006, 2011 Jean-loup Gailly. * For conditions of distribution and use, see copyright notice in zlib.h */ /* @(#) $Id$ */ #include "zlib.h" #include <stdio.h> #ifdef STDC # include <string.h> # include <stdlib.h> #endif #if defined(VMS) || defined(RISCOS) # define TESTFILE "foo-gz" #else # define TESTFILE "foo.gz" #endif #define CHECK_ERR(err, msg) { \ if (err != Z_OK) { \ fprintf(stderr, "%s error: %d\n", msg, err); \ exit(1); \ } \ } const char hello[] = "hello, hello!"; /* 字符长度为14(末尾还有一个null字符) */ /* "hello world" would be more standard, but the repeated "hello" * stresses the compression code better, sorry... */ const char dictionary[] = "hello"; uLong dictId; /* 字典的Adler32校验值 */ void test_deflate OF((Byte *compr, uLong comprLen)); void test_inflate OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); void test_large_deflate OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); void test_large_inflate OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); void test_flush OF((Byte *compr, uLong *comprLen)); void test_sync OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); void test_dict_deflate OF((Byte *compr, uLong comprLen)); void test_dict_inflate OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); int main OF((int argc, char *argv[])); /* Z_SOLO表示把zlib库编译成单独的不依赖第三方的库 */ #ifdef Z_SOLO /* 使用自定义的内存分配策略 */ void *myalloc OF((void *, unsigned, unsigned)); void myfree OF((void *, void *)); void *myalloc(q, n, m) void *q; unsigned n, m; { q = Z_NULL; return calloc(n, m); } void myfree(void *q, void *p) { q = Z_NULL; free(p); } static alloc_func zalloc = myalloc; static free_func zfree = myfree; #else /* !Z_SOLO */ /* 使用zlib默认的内存分配策略 */ static alloc_func zalloc = (alloc_func)0; static free_func zfree = (free_func)0;下面测试compress和uncompress的用法:
- voidtest_compressOF((Byte*compr,uLongcomprLen,
- Byte*uncompr,uLonguncomprLen));
- voidtest_gzioOF((constchar*fname,
- Byte*uncompr,uLonguncomprLen));
- /*===========================================================================
- *测试compress()和uncompress()
- */
- voidtest_compress(compr,comprLen,uncompr,uncomprLen)
- Byte*compr,*uncompr;
- uLongcomprLen,uncomprLen;
- {
- interr;
- uLonglen=(uLong)strlen(hello)+1;/*获取字符串长度*/
- /*压缩字符串*/
- err=compress(compr,&comprLen,(constBytef*)hello,len);
- CHECK_ERR(err,"compress");
- strcpy((char*)uncompr,"garbage");
- /*解压字符串*/
- err=uncompress(uncompr,&uncomprLen,compr,comprLen);
- CHECK_ERR(err,"uncompress");
- /*比较解压后的结果*/
- if(strcmp((char*)uncompr,hello)){
- fprintf(stderr,"baduncompress\n");
- exit(1);
- }else{
- printf("uncompress():%s\n",(char*)uncompr);
- }
- }
void test_compress OF((Byte *compr, uLong comprLen, Byte *uncompr, uLong uncomprLen)); void test_gzio OF((const char *fname, Byte *uncompr, uLong uncomprLen)); /* =========================================================================== * 测试compress()和uncompress() */ void test_compress(compr, comprLen, uncompr, uncomprLen) Byte *compr, *uncompr; uLong comprLen, uncomprLen; { int err; uLong len = (uLong)strlen(hello)+1; /* 获取字符串长度 */ /* 压缩字符串 */ err = compress(compr, &comprLen, (const Bytef*)hello, len); CHECK_ERR(err, "compress"); strcpy((char*)uncompr, "garbage"); /* 解压字符串 */ err = uncompress(uncompr, &uncomprLen, compr, comprLen); CHECK_ERR(err, "uncompress"); /* 比较解压后的结果 */ if (strcmp((char*)uncompr, hello)) { fprintf(stderr, "bad uncompress\n"); exit(1); } else { printf("uncompress(): %s\n", (char *)uncompr); } }下面测试gzip文件的读写操作:
- /*===========================================================================
- *测试.gz文件的读写操作
- */
- voidtest_gzio(fname,uncompr,uncomprLen)
- constchar*fname;/*gz文件名*/
- Byte*uncompr;
- uLonguncomprLen;
- {
- #ifdefNO_GZCOMPRESS
- fprintf(stderr,"NO_GZCOMPRESS--gz*functionscannotcompress\n");
- #else
- interr;
- intlen=(int)strlen(hello)+1;
- gzFilefile;
- z_off_tpos;
- file=gzopen(fname,"wb");/*打开要写入的gz文件*/
- if(file==NULL){
- fprintf(stderr,"gzopenerror\n");
- exit(1);
- }
- gzputc(file,'h');/*写入一个字符'h'*/
- if(gzputs(file,"ello")!=4){/*写入字符串"ello"*/
- fprintf(stderr,"gzputserr:%s\n",gzerror(file,&err));
- exit(1);
- }
- if(gzprintf(file,",%s!","hello")!=8){/*按格式写入字符串",hello!"*/
- fprintf(stderr,"gzprintferr:%s\n",gzerror(file,&err));
- exit(1);
- }
- gzseek(file,1L,SEEK_CUR);/*读写头向前移动1字节(即添加一个0字节)*/
- gzclose(file);/*关闭gz文件*/
- file=gzopen(fname,"rb");/*打开要读取的gz文件*/
- if(file==NULL){
- fprintf(stderr,"gzopenerror\n");
- exit(1);
- }
- strcpy((char*)uncompr,"garbage");
- /*从压缩文件中读取给定大小的解压字节数*/
- if(gzread(file,uncompr,(unsigned)uncomprLen)!=len){
- fprintf(stderr,"gzreaderr:%s\n",gzerror(file,&err));
- exit(1);
- }
- if(strcmp((char*)uncompr,hello)){/*比较解压后的结果*/
- fprintf(stderr,"badgzread:%s\n",(char*)uncompr);
- exit(1);
- }else{
- printf("gzread():%s\n",(char*)uncompr);
- }
- pos=gzseek(file,-8L,SEEK_CUR);/*读写头向后移动8字节,应该停留在第6个字符处*/
- if(pos!=6||gztell(file)!=pos){/*判断是否停留在第6个字符处*/
- fprintf(stderr,"gzseekerror,pos=%ld,gztell=%ld\n",
- (long)pos,(long)gztell(file));
- exit(1);
- }
- if(gzgetc(file)!=''){/*从当前位置读取1个字符,应该为字符''*/
- fprintf(stderr,"gzgetcerror\n");
- exit(1);
- }
- if(gzungetc('',file)!=''){/*推回这个字符到流中*/
- fprintf(stderr,"gzungetcerror\n");
- exit(1);
- }
- /*从压缩文件当前位置读取指定长度的解压字节数,直到len-1个字符被读取*/
- gzgets(file,(char*)uncompr,(int)uncomprLen);
- if(strlen((char*)uncompr)!=7){/*"hello!"*/
- fprintf(stderr,"gzgetserraftergzseek:%s\n",gzerror(file,&err));
- exit(1);
- }
- if(strcmp((char*)uncompr,hello+6)){
- fprintf(stderr,"badgzgetsaftergzseek\n");
- exit(1);
- }else{
- printf("gzgets()aftergzseek:%s\n",(char*)uncompr);
- }
- gzclose(file);/*关闭gz文件*/
- #endif
- }
- #endif/*Z_SOLO*/
/* =========================================================================== * 测试.gz文件的读写操作 */ void test_gzio(fname, uncompr, uncomprLen) const char *fname; /* gz文件名 */ Byte *uncompr; uLong uncomprLen; { #ifdef NO_GZCOMPRESS fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n"); #else int err; int len = (int)strlen(hello)+1; gzFile file; z_off_t pos; file = gzopen(fname, "wb"); /* 打开要写入的gz文件 */ if (file == NULL) { fprintf(stderr, "gzopen error\n"); exit(1); } gzputc(file, 'h'); /* 写入一个字符'h' */ if (gzputs(file, "ello") != 4) { /* 写入字符串"ello" */ fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err)); exit(1); } if (gzprintf(file, ", %s!", "hello") != 8) { /* 按格式写入字符串", hello!" */ fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err)); exit(1); } gzseek(file, 1L, SEEK_CUR); /* 读写头向前移动1字节(即添加一个0字节) */ gzclose(file); /* 关闭gz文件 */ file = gzopen(fname, "rb"); /* 打开要读取的gz文件 */ if (file == NULL) { fprintf(stderr, "gzopen error\n"); exit(1); } strcpy((char*)uncompr, "garbage"); /* 从压缩文件中读取给定大小的解压字节数 */ if (gzread(file, uncompr, (unsigned)uncomprLen) != len) { fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); exit(1); } if (strcmp((char*)uncompr, hello)) { /* 比较解压后的结果 */ fprintf(stderr, "bad gzread: %s\n", (char*)uncompr); exit(1); } else { printf("gzread(): %s\n", (char*)uncompr); } pos = gzseek(file, -8L, SEEK_CUR); /* 读写头向后移动8字节,应该停留在第6个字符处 */ if (pos != 6 || gztell(file) != pos) { /* 判断是否停留在第6个字符处 */ fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n", (long)pos, (long)gztell(file)); exit(1); } if (gzgetc(file) != ' ') { /* 从当前位置读取1个字符,应该为字符' ' */ fprintf(stderr, "gzgetc error\n"); exit(1); } if (gzungetc(' ', file) != ' ') { /* 推回这个字符到流中 */ fprintf(stderr, "gzungetc error\n"); exit(1); } /* 从压缩文件当前位置读取指定长度的解压字节数,直到len-1个字符被读取 */ gzgets(file, (char*)uncompr, (int)uncomprLen); if (strlen((char*)uncompr) != 7) { /* " hello!" */ fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err)); exit(1); } if (strcmp((char*)uncompr, hello + 6)) { fprintf(stderr, "bad gzgets after gzseek\n"); exit(1); } else { printf("gzgets() after gzseek: %s\n", (char*)uncompr); } gzclose(file); /* 关闭gz文件 */ #endif } #endif /* Z_SOLO */下面用小缓冲区测试压缩、解压操作(deflate/deflate):
- /*===========================================================================
- *测试deflate():使用小缓冲区
- */
- voidtest_deflate(compr,comprLen)
- Byte*compr;
- uLongcomprLen;
- {
- z_streamc_stream;/*压缩流*/
- interr;
- uLonglen=(uLong)strlen(hello)+1;
- /*这三个字段要在defalteInit之前初始化*/
- c_stream.zalloc=zalloc;
- c_stream.zfree=zfree;
- c_stream.opaque=(voidpf)0;
- /*初始化压缩流的状态,使用默认压缩级别*/
- err=deflateInit(&c_stream,Z_DEFAULT_COMPRESSION);
- CHECK_ERR(err,"deflateInit");
- /*设置压缩操作的输入数据和输出缓冲区*/
- c_stream.next_in=(Bytef*)hello;/*输入缓冲区指向输入字符串*/
- c_stream.next_out=compr;
- /*第一个循环:将flush设为Z_NO_FLUSH(表示还有输入数据未读完),将所有输入都读进去并进行压缩
- 根据avail_in和avail_out,不停地调用deflate将输入缓冲区的数据压缩
- 并写到输出缓冲区,直到输入字符串读完或输出缓冲区用完
- */
- while(c_stream.total_in!=len&&c_stream.total_out<comprLen){
- c_stream.avail_in=c_stream.avail_out=1;/*强制小缓冲区*/
- err=deflate(&c_stream,Z_NO_FLUSH);
- CHECK_ERR(err,"deflate");
- }
- /*第二个循环:将flush设置为Z_FINISH,不再输入,让deflate()完成全部的压缩输出
- 注意因为deflate压缩时可能是异步的(为了加速压缩,读取一次输入后不一定立刻就会产生压缩输出,
- 可能读完K字节后才会产生输出),所以上一个循环可能还没产生全部输出,需要这个循环,让flush保持Z_FINISH
- (表示输入数据已读完),多次调用deflate(),直到返回Z_STREAM_END,表示处理完全部输入并产生了全部的压缩输出
- */
- for(;;){/*完成压缩流的刷新,仍然强制小缓冲区*/
- c_stream.avail_out=1;
- err=deflate(&c_stream,Z_FINISH);
- if(err==Z_STREAM_END)break;
- CHECK_ERR(err,"deflate");
- }
- err=deflateEnd(&c_stream);/*释放压缩流的资源*/
- CHECK_ERR(err,"deflateEnd");
- }
- /*===========================================================================
- *测试inflate():使用小缓冲区
- */
- voidtest_inflate(compr,comprLen,uncompr,uncomprLen)
- Byte*compr,*uncompr;
- uLongcomprLen,uncomprLen;
- {
- interr;
- z_streamd_stream;/*解压流*/
- strcpy((char*)uncompr,"garbage");
- /*这些个字段要在infalteInit之前初始化*/
- d_stream.zalloc=zalloc;
- d_stream.zfree=zfree;
- d_stream.opaque=(voidpf)0;
- d_stream.next_in=compr;/*设置输入缓冲区*/
- d_stream.avail_in=0;
- d_stream.next_out=uncompr;/*设置输出缓冲区*/
- /*初始化解压流的状态*/
- err=inflateInit(&d_stream);
- CHECK_ERR(err,"inflateInit");
- /*只需一个循环:根据avail_in和avail_out,不停地调用inflate将输入缓冲区的数据
- 解压,直到返回Z_STREAM_END,表示处理完全部输入并产生了全部的解压输出
- 这里与flush参数是否为Z_FINISH无关
- */
- while(d_stream.total_out<uncomprLen&&d_stream.total_in<comprLen){
- d_stream.avail_in=d_stream.avail_out=1;/*强制小缓冲区*/
- err=inflate(&d_stream,Z_NO_FLUSH);
- if(err==Z_STREAM_END)break;
- CHECK_ERR(err,"inflate");
- }
- err=inflateEnd(&d_stream);/*释放解压流的资源*/
- CHECK_ERR(err,"inflateEnd");
- if(strcmp((char*)uncompr,hello)){/*比较解压后的数据*/
- fprintf(stderr,"badinflate\n");
- exit(1);
- }else{
- printf("inflate():%s\n",(char*)uncompr);
- }
- }
/* =========================================================================== * 测试deflate():使用小缓冲区 */ void test_deflate(compr, comprLen) Byte *compr; uLong comprLen; { z_stream c_stream; /* 压缩流 */ int err; uLong len = (uLong)strlen(hello)+1; /* 这三个字段要在defalteInit之前初始化 */ c_stream.zalloc = zalloc; c_stream.zfree = zfree; c_stream.opaque = (voidpf)0; /* 初始化压缩流的状态,使用默认压缩级别 */ err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); CHECK_ERR(err, "deflateInit"); /* 设置压缩操作的输入数据和输出缓冲区 */ c_stream.next_in = (Bytef*)hello; /* 输入缓冲区指向输入字符串 */ c_stream.next_out = compr; /* 第一个循环:将flush设为Z_NO_FLUSH(表示还有输入数据未读完),将所有输入都读进去并进行压缩 根据avail_in和avail_out,不停地调用deflate将输入缓冲区的数据压缩 并写到输出缓冲区,直到输入字符串读完或输出缓冲区用完 */ while (c_stream.total_in != len && c_stream.total_out < comprLen) { c_stream.avail_in = c_stream.avail_out = 1; /* 强制小缓冲区 */ err = deflate(&c_stream, Z_NO_FLUSH); CHECK_ERR(err, "deflate"); } /* 第二个循环:将flush设置为Z_FINISH,不再输入,让deflate()完成全部的压缩输出 注意因为deflate压缩时可能是异步的(为了加速压缩,读取一次输入后不一定立刻就会产生压缩输出, 可能读完K字节后才会产生输出),所以上一个循环可能还没产生全部输出,需要这个循环,让flush保持Z_FINISH (表示输入数据已读完),多次调用deflate(),直到返回Z_STREAM_END,表示处理完全部输入并产生了全部的压缩输出 */ for (;;) { /* 完成压缩流的刷新,仍然强制小缓冲区 */ c_stream.avail_out = 1; err = deflate(&c_stream, Z_FINISH); if (err == Z_STREAM_END) break; CHECK_ERR(err, "deflate"); } err = deflateEnd(&c_stream); /* 释放压缩流的资源 */ CHECK_ERR(err, "deflateEnd"); } /* =========================================================================== * 测试inflate():使用小缓冲区 */ void test_inflate(compr, comprLen, uncompr, uncomprLen) Byte *compr, *uncompr; uLong comprLen, uncomprLen; { int err; z_stream d_stream; /* 解压流 */ strcpy((char*)uncompr, "garbage"); /* 这些个字段要在infalteInit之前初始化 */ d_stream.zalloc = zalloc; d_stream.zfree = zfree; d_stream.opaque = (voidpf)0; d_stream.next_in = compr; /* 设置输入缓冲区 */ d_stream.avail_in = 0; d_stream.next_out = uncompr; /* 设置输出缓冲区 */ /* 初始化解压流的状态 */ err = inflateInit(&d_stream); CHECK_ERR(err, "inflateInit"); /* 只需一个循环:根据avail_in和avail_out,不停地调用inflate将输入缓冲区的数据 解压,直到返回Z_STREAM_END,表示处理完全部输入并产生了全部的解压输出 这里与flush参数是否为Z_FINISH无关 */ while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { d_stream.avail_in = d_stream.avail_out = 1; /* 强制小缓冲区 */ err = inflate(&d_stream, Z_NO_FLUSH); if (err == Z_STREAM_END) break; CHECK_ERR(err, "inflate"); } err = inflateEnd(&d_stream); /* 释放解压流的资源 */ CHECK_ERR(err, "inflateEnd"); if (strcmp((char*)uncompr, hello)) { /* 比较解压后的数据 */ fprintf(stderr, "bad inflate\n"); exit(1); } else { printf("inflate(): %s\n", (char *)uncompr); } }下面使用大缓冲区测试压缩、解压操作(deflate/deflate):
- /*===========================================================================
- *测试deflate():使用大缓冲区和动态改变的压缩级别
- */
- voidtest_large_deflate(compr,comprLen,uncompr,uncomprLen)
- Byte*compr,*uncompr;
- uLongcomprLen,uncomprLen;
- {
- z_streamc_stream;/*压缩流*/
- interr;
- /*这三个字段要在defalteInit之前初始化*/
- c_stream.zalloc=zalloc;
- c_stream.zfree=zfree;
- c_stream.opaque=(voidpf)0;
- /*初始化压缩流的状态,使用最快速度压缩*/
- err=deflateInit(&c_stream,Z_BEST_SPEED);
- CHECK_ERR(err,"deflateInit");
- c_stream.next_out=compr;
- c_stream.avail_out=(uInt)comprLen;
- /*这里,uncompr几乎都为0,因此可以很好地被压缩*/
- c_stream.next_in=uncompr;
- c_stream.avail_in=(uInt)uncomprLen;
- err=deflate(&c_stream,Z_NO_FLUSH);/*压缩输入数据*/
- CHECK_ERR(err,"deflate");
- if(c_stream.avail_in!=0){
- fprintf(stderr,"deflatenotgreedy\n");
- exit(1);
- }
- /*把已压缩的数据转换成未压缩:*/
- /*设置流的压缩级别(为未压缩)和压缩策略*/
- deflateParams(&c_stream,Z_NO_COMPRESSION,Z_DEFAULT_STRATEGY);
- c_stream.next_in=compr;
- c_stream.avail_in=(uInt)comprLen/2;
- err=deflate(&c_stream,Z_NO_FLUSH);
- CHECK_ERR(err,"deflate");
- /*转换回压缩模式(最高压缩率):*/
- deflateParams(&c_stream,Z_BEST_COMPRESSION,Z_FILTERED);
- c_stream.next_in=uncompr;
- c_stream.avail_in=(uInt)uncomprLen;
- err=deflate(&c_stream,Z_NO_FLUSH);
- CHECK_ERR(err,"deflate");
- /*流刷新,产生全部压缩输出*/
- err=deflate(&c_stream,Z_FINISH);
- if(err!=Z_STREAM_END){
- fprintf(stderr,"deflateshouldreportZ_STREAM_END\n");
- exit(1);
- }
- err=deflateEnd(&c_stream);/*释放流的资源*/
- CHECK_ERR(err,"deflateEnd");
- }
- /*===========================================================================
- *测试inflate():使用大缓冲区
- */
- voidtest_large_inflate(compr,comprLen,uncompr,uncomprLen)
- Byte*compr,*uncompr;
- uLongcomprLen,uncomprLen;
- {
- interr;
- z_streamd_stream;/*解压流*/
- strcpy((char*)uncompr,"garbage");
- /*这些个字段要在infalteInit之前初始化*/
- d_stream.zalloc=zalloc;
- d_stream.zfree=zfree;
- d_stream.opaque=(voidpf)0;
- d_stream.next_in=compr;
- d_stream.avail_in=(uInt)comprLen;
- /*初始化解压流*/
- err=inflateInit(&d_stream);
- CHECK_ERR(err,"inflateInit");
- /*解压*/
- for(;;){
- d_stream.next_out=uncompr;/*抛弃输出*/
- d_stream.avail_out=(uInt)uncomprLen;
- err=inflate(&d_stream,Z_NO_FLUSH);/*解压输入数据*/
- if(err==Z_STREAM_END)break;
- CHECK_ERR(err,"largeinflate");
- }
- err=inflateEnd(&d_stream);
- CHECK_ERR(err,"inflateEnd");
- if(d_stream.total_out!=2*uncomprLen+comprLen/2){
- fprintf(stderr,"badlargeinflate:%ld\n",d_stream.total_out);
- exit(1);
- }else{
- printf("large_inflate():OK\n");
- }
- }
/* =========================================================================== * 测试deflate():使用大缓冲区和动态改变的压缩级别 */ void test_large_deflate(compr, comprLen, uncompr, uncomprLen) Byte *compr, *uncompr; uLong comprLen, uncomprLen; { z_stream c_stream; /* 压缩流 */ int err; /* 这三个字段要在defalteInit之前初始化 */ c_stream.zalloc = zalloc; c_stream.zfree = zfree; c_stream.opaque = (voidpf)0; /* 初始化压缩流的状态,使用最快速度压缩 */ err = deflateInit(&c_stream, Z_BEST_SPEED); CHECK_ERR(err, "deflateInit"); c_stream.next_out = compr; c_stream.avail_out = (uInt)comprLen; /* 这里,uncompr几乎都为0,因此可以很好地被压缩 */ c_stream.next_in = uncompr; c_stream.avail_in = (uInt)uncomprLen; err = deflate(&c_stream, Z_NO_FLUSH); /* 压缩输入数据 */ CHECK_ERR(err, "deflate"); if (c_stream.avail_in != 0) { fprintf(stderr, "deflate not greedy\n"); exit(1); } /* 把已压缩的数据转换成未压缩: */ /* 设置流的压缩级别(为未压缩)和压缩策略 */ deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY); c_stream.next_in = compr; c_stream.avail_in = (uInt)comprLen/2; err = deflate(&c_stream, Z_NO_FLUSH); CHECK_ERR(err, "deflate"); /* 转换回压缩模式(最高压缩率): */ deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED); c_stream.next_in = uncompr; c_stream.avail_in = (uInt)uncomprLen; err = deflate(&c_stream, Z_NO_FLUSH); CHECK_ERR(err, "deflate"); /* 流刷新,产生全部压缩输出 */ err = deflate(&c_stream, Z_FINISH); if (err != Z_STREAM_END) { fprintf(stderr, "deflate should report Z_STREAM_END\n"); exit(1); } err = deflateEnd(&c_stream); /* 释放流的资源 */ CHECK_ERR(err, "deflateEnd"); } /* =========================================================================== * 测试inflate():使用大缓冲区 */ void test_large_inflate(compr, comprLen, uncompr, uncomprLen) Byte *compr, *uncompr; uLong comprLen, uncomprLen; { int err; z_stream d_stream; /* 解压流 */ strcpy((char*)uncompr, "garbage"); /* 这些个字段要在infalteInit之前初始化 */ d_stream.zalloc = zalloc; d_stream.zfree = zfree; d_stream.opaque = (voidpf)0; d_stream.next_in = compr; d_stream.avail_in = (uInt)comprLen; /* 初始化解压流 */ err = inflateInit(&d_stream); CHECK_ERR(err, "inflateInit"); /* 解压 */ for (;;) { d_stream.next_out = uncompr; /* 抛弃输出 */ d_stream.avail_out = (uInt)uncomprLen; err = inflate(&d_stream, Z_NO_FLUSH); /* 解压输入数据 */ if (err == Z_STREAM_END) break; CHECK_ERR(err, "large inflate"); } err = inflateEnd(&d_stream); CHECK_ERR(err, "inflateEnd"); if (d_stream.total_out != 2*uncomprLen + comprLen/2) { fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out); exit(1); } else { printf("large_inflate(): OK\n"); } }下面使用完全刷新模式测试压缩操作deflate:
- /*===========================================================================
- *测试deflate():使用完全刷新
- */
- voidtest_flush(compr,comprLen)
- Byte*compr;
- uLong*comprLen;
- {
- z_streamc_stream;/*压缩流*/
- interr;
- uIntlen=(uInt)strlen(hello)+1;
- c_stream.zalloc=zalloc;
- c_stream.zfree=zfree;
- c_stream.opaque=(voidpf)0;
- err=deflateInit(&c_stream,Z_DEFAULT_COMPRESSION);
- CHECK_ERR(err,"deflateInit");
- c_stream.next_in=(Bytef*)hello;
- c_stream.next_out=compr;
- c_stream.avail_in=3;
- c_stream.avail_out=(uInt)*comprLen;
- /*使用完全刷新来压缩字符串*/
- err=deflate(&c_stream,Z_FULL_FLUSH);
- CHECK_ERR(err,"deflate");
- compr[3]++;/*在第一个压缩块中强制产生一个错误*/
- c_stream.avail_in=len-3;
- err=deflate(&c_stream,Z_FINISH);
- if(err!=Z_STREAM_END){
- CHECK_ERR(err,"deflate");
- }
- err=deflateEnd(&c_stream);
- CHECK_ERR(err,"deflateEnd");
- *comprLen=c_stream.total_out;
- }
/* =========================================================================== * 测试deflate():使用完全刷新 */ void test_flush(compr, comprLen) Byte *compr; uLong *comprLen; { z_stream c_stream; /* 压缩流 */ int err; uInt len = (uInt)strlen(hello)+1; c_stream.zalloc = zalloc; c_stream.zfree = zfree; c_stream.opaque = (voidpf)0; err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); CHECK_ERR(err, "deflateInit"); c_stream.next_in = (Bytef*)hello; c_stream.next_out = compr; c_stream.avail_in = 3; c_stream.avail_out = (uInt)*comprLen; /* 使用完全刷新来压缩字符串 */ err = deflate(&c_stream, Z_FULL_FLUSH); CHECK_ERR(err, "deflate"); compr[3]++; /* 在第一个压缩块中强制产生一个错误 */ c_stream.avail_in = len - 3; err = deflate(&c_stream, Z_FINISH); if (err != Z_STREAM_END) { CHECK_ERR(err, "deflate"); } err = deflateEnd(&c_stream); CHECK_ERR(err, "deflateEnd"); *comprLen = c_stream.total_out; }下面测试同步方式的解压操作inflateSync:
- /*===========================================================================
- *TestinflateSync()
- */
- voidtest_sync(compr,comprLen,uncompr,uncomprLen)
- Byte*compr,*uncompr;
- uLongcomprLen,uncomprLen;
- {
- interr;
- z_streamd_stream;/*解压流*/
- strcpy((char*)uncompr,"garbage");
- d_stream.zalloc=zalloc;
- d_stream.zfree=zfree;
- d_stream.opaque=(voidpf)0;
- d_stream.next_in=compr;/*设置输入缓冲区*/
- d_stream.avail_in=2;/*只读取zlib头部信息*/
- err=inflateInit(&d_stream);
- CHECK_ERR(err,"inflateInit");
- d_stream.next_out=uncompr;/*设置输出缓冲区*/
- d_stream.avail_out=(uInt)uncomprLen;
- inflate(&d_stream,Z_NO_FLUSH);
- CHECK_ERR(err,"inflate");
- d_stream.avail_in=(uInt)comprLen-2;/*读取所有压缩数据*/
- err=inflateSync(&d_stream);/*但忽略损坏的部分*/
- CHECK_ERR(err,"inflateSync");
- err=inflate(&d_stream,Z_FINISH);/*完成解压*/
- if(err!=Z_DATA_ERROR){
- fprintf(stderr,"inflateshouldreportDATA_ERROR\n");
- /*因为不正确的adler32*/
- exit(1);
- }
- err=inflateEnd(&d_stream);
- CHECK_ERR(err,"inflateEnd");
- printf("afterinflateSync():hel%s\n",(char*)uncompr);
- }
/* =========================================================================== * Test inflateSync() */ void test_sync(compr, comprLen, uncompr, uncomprLen) Byte *compr, *uncompr; uLong comprLen, uncomprLen; { int err; z_stream d_stream; /* 解压流 */ strcpy((char*)uncompr, "garbage"); d_stream.zalloc = zalloc; d_stream.zfree = zfree; d_stream.opaque = (voidpf)0; d_stream.next_in = compr; /* 设置输入缓冲区 */ d_stream.avail_in = 2; /* 只读取zlib头部信息 */ err = inflateInit(&d_stream); CHECK_ERR(err, "inflateInit"); d_stream.next_out = uncompr; /* 设置输出缓冲区 */ d_stream.avail_out = (uInt)uncomprLen; inflate(&d_stream, Z_NO_FLUSH); CHECK_ERR(err, "inflate"); d_stream.avail_in = (uInt)comprLen-2; /* 读取所有压缩数据 */ err = inflateSync(&d_stream); /* 但忽略损坏的部分 */ CHECK_ERR(err, "inflateSync"); err = inflate(&d_stream, Z_FINISH); /* 完成解压 */ if (err != Z_DATA_ERROR) { fprintf(stderr, "inflate should report DATA_ERROR\n"); /* 因为不正确的adler32 */ exit(1); } err = inflateEnd(&d_stream); CHECK_ERR(err, "inflateEnd"); printf("after inflateSync(): hel%s\n", (char *)uncompr); }下面用预设的字典测试压缩、解压操作(deflate/inflate):
- /*===========================================================================
- *测试:deflate():使用预设的字典
- */
- voidtest_dict_deflate(compr,comprLen)
- Byte*compr;
- uLongcomprLen;
- {
- z_streamc_stream;/*压缩流*/
- interr;
- c_stream.zalloc=zalloc;
- c_stream.zfree=zfree;
- c_stream.opaque=(voidpf)0;
- err=deflateInit(&c_stream,Z_BEST_COMPRESSION);
- CHECK_ERR(err,"deflateInit");
- /*设置压缩流要使用的字典*/
- err=deflateSetDictionary(&c_stream,
- (constBytef*)dictionary,(int)sizeof(dictionary));
- CHECK_ERR(err,"deflateSetDictionary");
- dictId=c_stream.adler;/*得到字典的Alder32校验值*/
- c_stream.next_out=compr;
- c_stream.avail_out=(uInt)comprLen;
- c_stream.next_in=(Bytef*)hello;/*输入要压缩的字符串*/
- c_stream.avail_in=(uInt)strlen(hello)+1;
- /*直接进行压缩*/
- err=deflate(&c_stream,Z_FINISH);
- if(err!=Z_STREAM_END){
- fprintf(stderr,"deflateshouldreportZ_STREAM_END\n");
- exit(1);
- }
- err=deflateEnd(&c_stream);
- CHECK_ERR(err,"deflateEnd");
- }
- /*===========================================================================
- *测试inflate():使用预设的字典
- */
- voidtest_dict_inflate(compr,comprLen,uncompr,uncomprLen)
- Byte*compr,*uncompr;
- uLongcomprLen,uncomprLen;
- {
- interr;
- z_streamd_stream;/*解压流*/
- strcpy((char*)uncompr,"garbage");
- d_stream.zalloc=zalloc;
- d_stream.zfree=zfree;
- d_stream.opaque=(voidpf)0;
- d_stream.next_in=compr;
- d_stream.avail_in=(uInt)comprLen;
- err=inflateInit(&d_stream);
- CHECK_ERR(err,"inflateInit");
- d_stream.next_out=uncompr;
- d_stream.avail_out=(uInt)uncomprLen;
- for(;;){/*解压*/
- err=inflate(&d_stream,Z_NO_FLUSH);
- if(err==Z_STREAM_END)break;
- if(err==Z_NEED_DICT){/*如果需要字典*/
- if(d_stream.adler!=dictId){/*校验是否与压缩时的字典值一致*/
- fprintf(stderr,"unexpecteddictionary");
- exit(1);
- }
- /*设置解压需要的字典*/
- err=inflateSetDictionary(&d_stream,(constBytef*)dictionary,
- (int)sizeof(dictionary));
- }
- CHECK_ERR(err,"inflatewithdict");
- }
- err=inflateEnd(&d_stream);
- CHECK_ERR(err,"inflateEnd");
- if(strcmp((char*)uncompr,hello)){/*比较解压后的字符串*/
- fprintf(stderr,"badinflatewithdict\n");
- exit(1);
- }else{
- printf("inflatewithdictionary:%s\n",(char*)uncompr);
- }
- }
/* =========================================================================== * 测试:deflate():使用预设的字典 */ void test_dict_deflate(compr, comprLen) Byte *compr; uLong comprLen; { z_stream c_stream; /* 压缩流 */ int err; c_stream.zalloc = zalloc; c_stream.zfree = zfree; c_stream.opaque = (voidpf)0; err = deflateInit(&c_stream, Z_BEST_COMPRESSION); CHECK_ERR(err, "deflateInit"); /* 设置压缩流要使用的字典 */ err = deflateSetDictionary(&c_stream, (const Bytef*)dictionary, (int)sizeof(dictionary)); CHECK_ERR(err, "deflateSetDictionary"); dictId = c_stream.adler; /* 得到字典的Alder32校验值 */ c_stream.next_out = compr; c_stream.avail_out = (uInt)comprLen; c_stream.next_in = (Bytef*)hello; /* 输入要压缩的字符串 */ c_stream.avail_in = (uInt)strlen(hello)+1; /* 直接进行压缩 */ err = deflate(&c_stream, Z_FINISH); if (err != Z_STREAM_END) { fprintf(stderr, "deflate should report Z_STREAM_END\n"); exit(1); } err = deflateEnd(&c_stream); CHECK_ERR(err, "deflateEnd"); } /* =========================================================================== * 测试inflate():使用预设的字典 */ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen) Byte *compr, *uncompr; uLong comprLen, uncomprLen; { int err; z_stream d_stream; /* 解压流 */ strcpy((char*)uncompr, "garbage"); d_stream.zalloc = zalloc; d_stream.zfree = zfree; d_stream.opaque = (voidpf)0; d_stream.next_in = compr; d_stream.avail_in = (uInt)comprLen; err = inflateInit(&d_stream); CHECK_ERR(err, "inflateInit"); d_stream.next_out = uncompr; d_stream.avail_out = (uInt)uncomprLen; for (;;) { /* 解压 */ err = inflate(&d_stream, Z_NO_FLUSH); if (err == Z_STREAM_END) break; if (err == Z_NEED_DICT) { /* 如果需要字典 */ if (d_stream.adler != dictId) { /* 校验是否与压缩时的字典值一致 */ fprintf(stderr, "unexpected dictionary"); exit(1); } /* 设置解压需要的字典 */ err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary, (int)sizeof(dictionary)); } CHECK_ERR(err, "inflate with dict"); } err = inflateEnd(&d_stream); CHECK_ERR(err, "inflateEnd"); if (strcmp((char*)uncompr, hello)) { /* 比较解压后的字符串 */ fprintf(stderr, "bad inflate with dict\n"); exit(1); } else { printf("inflate with dictionary: %s\n", (char *)uncompr); } }下面是命令行程序:
- /*===========================================================================
- *Usage:example[output.gz[input.gz]]
- */
- intmain(argc,argv)
- intargc;
- char*argv[];
- {
- Byte*compr,*uncompr;
- uLongcomprLen=10000*sizeof(int);/*在MSDOS上不会溢出*/
- uLonguncomprLen=comprLen;
- staticconstchar*myVersion=ZLIB_VERSION;
- /*检查zlib版本是否一致*/
- if(zlibVersion()[0]!=myVersion[0]){
- fprintf(stderr,"incompatiblezlibversion\n");
- exit(1);
- }elseif(strcmp(zlibVersion(),ZLIB_VERSION)!=0){
- fprintf(stderr,"warning:differentzlibversion\n");
- }
- /*打印版本和zlib编译信息*/
- printf("zlibversion%s=0x%04x,compileflags=0x%lx\n",
- ZLIB_VERSION,ZLIB_VERNUM,zlibCompileFlags());
- /*分配输入、输出缓冲区的内存*/
- compr=(Byte*)calloc((uInt)comprLen,1);
- uncompr=(Byte*)calloc((uInt)uncomprLen,1);
- /*清空compr和uncompr,以避免读到未初始化的数据,并且确保uncompr能很好
- *地被压缩
- */
- if(compr==Z_NULL||uncompr==Z_NULL){
- printf("outofmemory\n");
- exit(1);
- }
- /*下面运行各个测试函数*/
- #ifdefZ_SOLO
- argc=strlen(argv[0]);
- #else
- test_compress(compr,comprLen,uncompr,uncomprLen);
- test_gzio((argc>1?argv[1]:TESTFILE),
- uncompr,uncomprLen);
- #endif
- test_deflate(compr,comprLen);
- test_inflate(compr,comprLen,uncompr,uncomprLen);
- test_large_deflate(compr,comprLen,uncompr,uncomprLen);
- test_large_inflate(compr,comprLen,uncompr,uncomprLen);
- test_flush(compr,&comprLen);
- test_sync(compr,comprLen,uncompr,uncomprLen);
- comprLen=uncomprLen;
- test_dict_deflate(compr,comprLen);
- test_dict_inflate(compr,comprLen,uncompr,uncomprLen);
- /*释放缓冲区资源*/
- free(compr);
- free(uncompr);
- return0;
- }
/* =========================================================================== * Usage: example [output.gz [input.gz]] */ int main(argc, argv) int argc; char *argv[]; { Byte *compr, *uncompr; uLong comprLen = 10000*sizeof(int); /* 在MSDOS上不会溢出 */ uLong uncomprLen = comprLen; static const char* myVersion = ZLIB_VERSION; /* 检查zlib版本是否一致 */ if (zlibVersion()[0] != myVersion[0]) { fprintf(stderr, "incompatible zlib version\n"); exit(1); } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) { fprintf(stderr, "warning: different zlib version\n"); } /* 打印版本和zlib编译信息 */ printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n", ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags()); /* 分配输入、输出缓冲区的内存 */ compr = (Byte*)calloc((uInt)comprLen, 1); uncompr = (Byte*)calloc((uInt)uncomprLen, 1); /* 清空compr和uncompr,以避免读到未初始化的数据,并且确保uncompr能很好 * 地被压缩 */ if (compr == Z_NULL || uncompr == Z_NULL) { printf("out of memory\n"); exit(1); } /* 下面运行各个测试函数 */ #ifdef Z_SOLO argc = strlen(argv[0]); #else test_compress(compr, comprLen, uncompr, uncomprLen); test_gzio((argc > 1 ? argv[1] : TESTFILE), uncompr, uncomprLen); #endif test_deflate(compr, comprLen); test_inflate(compr, comprLen, uncompr, uncomprLen); test_large_deflate(compr, comprLen, uncompr, uncomprLen); test_large_inflate(compr, comprLen, uncompr, uncomprLen); test_flush(compr, &comprLen); test_sync(compr, comprLen, uncompr, uncomprLen); comprLen = uncomprLen; test_dict_deflate(compr, comprLen); test_dict_inflate(compr, comprLen, uncompr, uncomprLen); /* 释放缓冲区资源 */ free(compr); free(uncompr); return 0; }
相关推荐
7. `example.c`:示例代码,展示了如何在实际项目中使用zlib库。 8. `inftrees.c`:处理解压缩时的树结构操作,与`trees.c`相辅相成。 9. `inffast.c`:快速解压缩实现,通过优化算法来提高解压速度。 10. `crc32.c`...
- `example.c`:包含了一些示例代码,展示了如何使用`zlib`库进行压缩和解压缩操作。 - `inftrees.c`:处理`deflate`编码中用到的动态霍夫曼树。 - `crc32.c`:实现了CRC32校验码的计算,用于验证数据的完整性和一致...
在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 localhost$ ssh -l jsmith remotehost....
1. **日志管理**:配置日志文件位置和级别,便于监控和分析。 2. **安全设置**:启用HTTPS,使用SSL证书加密通信,限制访问权限。 3. **缓存设置**:配置静态文件缓存,提高响应速度。 4. **负载均衡**:通过Nginx的...
HDF5(Hierarchical Data Format 5)是一种高级数据存储格式,被广泛应用于科学计算、数据分析和工程领域。它提供了高效、灵活和可扩展的方式来组织和管理大量的多维数据。在C++环境中,HDF5库允许开发人员创建、...
2. 打开 ZIP 文件:使用库提供的函数打开 ZIP 文件,可能需要指定文件路径和操作模式(如只读、读写)。 3. 遍历文件:获取 ZIP 文件中的所有条目(entries),通常是一个包含文件信息的列表。 4. 读取或写入:根据...
- 写入变量:使用`matioWriteVariable`将C数据类型转换为MATLAB兼容的数据类型,并写入文件。 - 读取变量:使用`matioReadVariable`从文件中读取变量。 - 关闭文件:使用`matioClose`关闭文件。 **6. 示例代码**...
3. **Example项目**:示例代码,展示libuv的使用方法。 四、编译与运行 在E:\Aubo\libuv-v1.39.0.tar目录下,首先使用CMake配置生成.sln文件,然后使用Visual Studio打开.sln,选择适当的配置(如Debug或Release)...
- `example.js`:可能包含了一些示例代码,展示了如何使用流处理数据。 - `test`目录:可能包含了测试文件,用于验证服务器功能的正确性。 通过分析这些文件,你可以更深入地了解流服务器的实现细节。例如,`server...
4. **构建依赖**:检查源代码中的编译依赖项,如OpenSSL、zlib等,确保目标系统已安装这些库或准备安装它们。有时需要交叉编译这些库以适应目标环境。 5. **编译选项**:配置编译选项以适应目标系统。使用 `./...
- 使用Transform流,比如zlib模块的Gzip或Gunzip,对数据进行压缩或解压缩。 - 处理HTTP请求和响应时,利用内置的http模块,可以看到流的用法。 - 可能还有自定义的双工流,用于自定义的数据处理和传输逻辑。 五、...
描述中的"win 32 touch example"可能是指一个使用Win32 API来处理触摸输入功能的示例,这在现代的Windows操作系统中,尤其是支持触摸屏设备的系统中是常见的需求。这里的“touch”可能意味着示例代码会展示如何在Win...
《Pillow库详解及其在Python中的应用》 Pillow,作为一个Python图像处理库,是 PIL(Python Imaging Library)的一个分支,旨在提供更加友好且易于安装的接口,支持Python 3.x版本。在“Pillow-2.3.2.tar.gz”这个...
Minizip库是对Zlib库的扩展,Zlib是一个广泛使用的数据压缩库。 2. **QtC++接口的引入** Quazip将minizip的功能包装成C++类,使其更符合Qt的编程模式。这意味着开发者可以使用Qt的I/O设备接口,如QFile或...
- 使用汇编语言调用高级语言的库:可能需要使用系统调用或DLL导入来利用已经存在的ZIP处理库,如zlib,因为直接在Asm中实现完整的压缩和解压缩功能可能非常复杂。 总之,“lab_05_zip_Asm_”的学习涵盖了汇编语言...