zlib库剖析(4):使用示例example.c

    下面分析test/example.c,它示范了zlib库的各个函数的使用。

    下面代码定义要压缩的字符串、压缩时使用的字典、压缩/解压缩的内存分配策略等。

  1. /* example.c -- usage example of the zlib compression library 
  2.  * Copyright (C) 1995-2006, 2011 Jean-loup Gailly. 
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */  
  5.   
  6. /* @(#) $Id$ */  
  7.   
  8. #include "zlib.h"   
  9. #include <stdio.h>   
  10.   
  11. #ifdef STDC   
  12. #  include <string.h>   
  13. #  include <stdlib.h>   
  14. #endif   
  15.   
  16. #if defined(VMS) || defined(RISCOS)   
  17. #  define TESTFILE "foo-gz"   
  18. #else   
  19. #  define TESTFILE "foo.gz"   
  20. #endif   
  21.   
  22. #define CHECK_ERR(err, msg) { \   
  23.     if (err != Z_OK) { \  
  24.         fprintf(stderr, "%s error: %d\n", msg, err); \  
  25.         exit(1); \  
  26.     } \  
  27. }  
  28.   
  29. const char hello[] = "hello, hello!";  /* 字符长度为14(末尾还有一个null字符) */  
  30. /* "hello world" would be more standard, but the repeated "hello" 
  31.  * stresses the compression code better, sorry... 
  32.  */  
  33.   
  34. const char dictionary[] = "hello";  
  35. uLong dictId; /* 字典的Adler32校验值 */  
  36.   
  37. void test_deflate       OF((Byte *compr, uLong comprLen));  
  38. void test_inflate       OF((Byte *compr, uLong comprLen,  
  39.                             Byte *uncompr, uLong uncomprLen));  
  40. void test_large_deflate OF((Byte *compr, uLong comprLen,  
  41.                             Byte *uncompr, uLong uncomprLen));  
  42. void test_large_inflate OF((Byte *compr, uLong comprLen,  
  43.                             Byte *uncompr, uLong uncomprLen));  
  44. void test_flush         OF((Byte *compr, uLong *comprLen));  
  45. void test_sync          OF((Byte *compr, uLong comprLen,  
  46.                             Byte *uncompr, uLong uncomprLen));  
  47. void test_dict_deflate  OF((Byte *compr, uLong comprLen));  
  48. void test_dict_inflate  OF((Byte *compr, uLong comprLen,  
  49.                             Byte *uncompr, uLong uncomprLen));  
  50. int  main               OF((int argc, char *argv[]));  
  51.   
  52. /* Z_SOLO表示把zlib库编译成单独的不依赖第三方的库 */  
  53. #ifdef Z_SOLO   
  54.   
  55. /* 使用自定义的内存分配策略 */  
  56. void *myalloc OF((void *, unsigned, unsigned));  
  57. void myfree OF((void *, void *));  
  58.   
  59. void *myalloc(q, n, m)  
  60.     void *q;  
  61.     unsigned n, m;  
  62. {  
  63.     q = Z_NULL;  
  64.     return calloc(n, m);  
  65. }  
  66.   
  67. void myfree(void *q, void *p)  
  68. {  
  69.     q = Z_NULL;  
  70.     free(p);  
  71. }  
  72.   
  73. static alloc_func zalloc = myalloc;  
  74. static free_func zfree = myfree;  
  75.   
  76. #else /* !Z_SOLO */   
  77.   
  78. /* 使用zlib默认的内存分配策略 */  
  79. static alloc_func zalloc = (alloc_func)0;  
  80. static free_func zfree = (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的用法:
  1. void test_compress      OF((Byte *compr, uLong comprLen,  
  2.                             Byte *uncompr, uLong uncomprLen));  
  3. void test_gzio          OF((const char *fname,  
  4.                             Byte *uncompr, uLong uncomprLen));  
  5.   
  6. /* =========================================================================== 
  7.  * 测试compress()和uncompress() 
  8.  */  
  9. void test_compress(compr, comprLen, uncompr, uncomprLen)  
  10.     Byte *compr, *uncompr;  
  11.     uLong comprLen, uncomprLen;  
  12. {  
  13.     int err;  
  14.     uLong len = (uLong)strlen(hello)+1; /* 获取字符串长度 */  
  15.       
  16.     /* 压缩字符串 */  
  17.     err = compress(compr, &comprLen, (const Bytef*)hello, len);  
  18.     CHECK_ERR(err, "compress");  
  19.   
  20.     strcpy((char*)uncompr, "garbage");  
  21.       
  22.     /* 解压字符串 */  
  23.     err = uncompress(uncompr, &uncomprLen, compr, comprLen);  
  24.     CHECK_ERR(err, "uncompress");  
  25.       
  26.     /* 比较解压后的结果 */  
  27.     if (strcmp((char*)uncompr, hello)) {  
  28.         fprintf(stderr, "bad uncompress\n");  
  29.         exit(1);  
  30.     } else {  
  31.         printf("uncompress(): %s\n", (char *)uncompr);  
  32.     }  
  33. }  
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文件的读写操作:
  1. /* =========================================================================== 
  2.  * 测试.gz文件的读写操作 
  3.  */  
  4. void test_gzio(fname, uncompr, uncomprLen)  
  5.     const char *fname; /* gz文件名 */  
  6.     Byte *uncompr;  
  7.     uLong uncomprLen;  
  8. {  
  9. #ifdef NO_GZCOMPRESS   
  10.     fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");  
  11. #else   
  12.     int err;  
  13.     int len = (int)strlen(hello)+1;  
  14.     gzFile file;  
  15.     z_off_t pos;  
  16.   
  17.     file = gzopen(fname, "wb");  /* 打开要写入的gz文件 */  
  18.     if (file == NULL) {  
  19.         fprintf(stderr, "gzopen error\n");  
  20.         exit(1);  
  21.     }  
  22.     gzputc(file, 'h');  /* 写入一个字符'h' */  
  23.     if (gzputs(file, "ello") != 4) {  /* 写入字符串"ello" */  
  24.         fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));  
  25.         exit(1);  
  26.     }  
  27.     if (gzprintf(file, ", %s!""hello") != 8) { /* 按格式写入字符串", hello!" */  
  28.         fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));  
  29.         exit(1);  
  30.     }  
  31.     gzseek(file, 1L, SEEK_CUR); /* 读写头向前移动1字节(即添加一个0字节) */  
  32.     gzclose(file);  /* 关闭gz文件 */  
  33.   
  34.     file = gzopen(fname, "rb");  /* 打开要读取的gz文件 */  
  35.     if (file == NULL) {  
  36.         fprintf(stderr, "gzopen error\n");  
  37.         exit(1);  
  38.     }  
  39.     strcpy((char*)uncompr, "garbage");  
  40.       
  41.     /* 从压缩文件中读取给定大小的解压字节数 */  
  42.     if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {  
  43.         fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));  
  44.         exit(1);  
  45.     }  
  46.     if (strcmp((char*)uncompr, hello)) {  /* 比较解压后的结果 */  
  47.         fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);  
  48.         exit(1);  
  49.     } else {  
  50.         printf("gzread(): %s\n", (char*)uncompr);  
  51.     }  
  52.   
  53.     pos = gzseek(file, -8L, SEEK_CUR);  /* 读写头向后移动8字节,应该停留在第6个字符处 */  
  54.     if (pos != 6 || gztell(file) != pos) {  /* 判断是否停留在第6个字符处 */  
  55.         fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",  
  56.                 (long)pos, (long)gztell(file));  
  57.         exit(1);  
  58.     }  
  59.   
  60.     if (gzgetc(file) != ' ') {  /* 从当前位置读取1个字符,应该为字符' ' */  
  61.         fprintf(stderr, "gzgetc error\n");  
  62.         exit(1);  
  63.     }  
  64.   
  65.     if (gzungetc(' ', file) != ' ') {  /* 推回这个字符到流中 */  
  66.         fprintf(stderr, "gzungetc error\n");  
  67.         exit(1);  
  68.     }  
  69.       
  70.     /* 从压缩文件当前位置读取指定长度的解压字节数,直到len-1个字符被读取 */  
  71.     gzgets(file, (char*)uncompr, (int)uncomprLen);  
  72.     if (strlen((char*)uncompr) != 7) { /* " hello!" */  
  73.         fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));  
  74.         exit(1);  
  75.     }  
  76.     if (strcmp((char*)uncompr, hello + 6)) {  
  77.         fprintf(stderr, "bad gzgets after gzseek\n");  
  78.         exit(1);  
  79.     } else {  
  80.         printf("gzgets() after gzseek: %s\n", (char*)uncompr);  
  81.     }  
  82.   
  83.     gzclose(file);  /* 关闭gz文件 */  
  84. #endif   
  85. }  
  86.   
  87. #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):
  1. /* =========================================================================== 
  2.  * 测试deflate():使用小缓冲区 
  3.  */  
  4. void test_deflate(compr, comprLen)  
  5.     Byte *compr;  
  6.     uLong comprLen;  
  7. {  
  8.     z_stream c_stream; /* 压缩流 */  
  9.     int err;  
  10.     uLong len = (uLong)strlen(hello)+1;  
  11.       
  12.     /* 这三个字段要在defalteInit之前初始化 */  
  13.     c_stream.zalloc = zalloc;  
  14.     c_stream.zfree = zfree;  
  15.     c_stream.opaque = (voidpf)0;  
  16.       
  17.     /* 初始化压缩流的状态,使用默认压缩级别 */  
  18.     err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);  
  19.     CHECK_ERR(err, "deflateInit");  
  20.       
  21.     /* 设置压缩操作的输入数据和输出缓冲区 */  
  22.     c_stream.next_in  = (Bytef*)hello;  /* 输入缓冲区指向输入字符串 */  
  23.     c_stream.next_out = compr;  
  24.   
  25.     /* 第一个循环:将flush设为Z_NO_FLUSH(表示还有输入数据未读完),将所有输入都读进去并进行压缩 
  26.        根据avail_in和avail_out,不停地调用deflate将输入缓冲区的数据压缩 
  27.        并写到输出缓冲区,直到输入字符串读完或输出缓冲区用完  
  28.     */  
  29.     while (c_stream.total_in != len && c_stream.total_out < comprLen) {  
  30.         c_stream.avail_in = c_stream.avail_out = 1; /* 强制小缓冲区 */  
  31.         err = deflate(&c_stream, Z_NO_FLUSH);  
  32.         CHECK_ERR(err, "deflate");  
  33.     }  
  34.     /* 第二个循环:将flush设置为Z_FINISH,不再输入,让deflate()完成全部的压缩输出 
  35.        注意因为deflate压缩时可能是异步的(为了加速压缩,读取一次输入后不一定立刻就会产生压缩输出, 
  36.        可能读完K字节后才会产生输出),所以上一个循环可能还没产生全部输出,需要这个循环,让flush保持Z_FINISH 
  37.        (表示输入数据已读完),多次调用deflate(),直到返回Z_STREAM_END,表示处理完全部输入并产生了全部的压缩输出  
  38.     */  
  39.     for (;;) {  /* 完成压缩流的刷新,仍然强制小缓冲区 */  
  40.         c_stream.avail_out = 1;  
  41.         err = deflate(&c_stream, Z_FINISH);  
  42.         if (err == Z_STREAM_END) break;  
  43.         CHECK_ERR(err, "deflate");  
  44.     }  
  45.   
  46.     err = deflateEnd(&c_stream);  /* 释放压缩流的资源 */  
  47.     CHECK_ERR(err, "deflateEnd");  
  48. }  
  49.   
  50. /* =========================================================================== 
  51.  * 测试inflate():使用小缓冲区 
  52.  */  
  53. void test_inflate(compr, comprLen, uncompr, uncomprLen)  
  54.     Byte *compr, *uncompr;  
  55.     uLong comprLen, uncomprLen;  
  56. {  
  57.     int err;  
  58.     z_stream d_stream; /* 解压流 */  
  59.   
  60.     strcpy((char*)uncompr, "garbage");  
  61.   
  62.     /* 这些个字段要在infalteInit之前初始化 */  
  63.     d_stream.zalloc = zalloc;  
  64.     d_stream.zfree = zfree;  
  65.     d_stream.opaque = (voidpf)0;  
  66.   
  67.     d_stream.next_in  = compr;  /* 设置输入缓冲区 */  
  68.     d_stream.avail_in = 0;  
  69.     d_stream.next_out = uncompr;  /* 设置输出缓冲区 */  
  70.   
  71.     /* 初始化解压流的状态 */  
  72.     err = inflateInit(&d_stream);  
  73.     CHECK_ERR(err, "inflateInit");  
  74.   
  75.     /* 只需一个循环:根据avail_in和avail_out,不停地调用inflate将输入缓冲区的数据 
  76.        解压,直到返回Z_STREAM_END,表示处理完全部输入并产生了全部的解压输出 
  77.        这里与flush参数是否为Z_FINISH无关 
  78.     */  
  79.     while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {  
  80.         d_stream.avail_in = d_stream.avail_out = 1; /* 强制小缓冲区 */  
  81.         err = inflate(&d_stream, Z_NO_FLUSH);  
  82.         if (err == Z_STREAM_END) break;  
  83.         CHECK_ERR(err, "inflate");  
  84.     }  
  85.   
  86.     err = inflateEnd(&d_stream);  /* 释放解压流的资源 */  
  87.     CHECK_ERR(err, "inflateEnd");  
  88.   
  89.     if (strcmp((char*)uncompr, hello)) {  /* 比较解压后的数据 */  
  90.         fprintf(stderr, "bad inflate\n");  
  91.         exit(1);  
  92.     } else {  
  93.         printf("inflate(): %s\n", (char *)uncompr);  
  94.     }  
  95. }  
/* ===========================================================================
 * 测试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):
  1. /* =========================================================================== 
  2.  * 测试deflate():使用大缓冲区和动态改变的压缩级别 
  3.  */  
  4. void test_large_deflate(compr, comprLen, uncompr, uncomprLen)  
  5.     Byte *compr, *uncompr;  
  6.     uLong comprLen, uncomprLen;  
  7. {  
  8.     z_stream c_stream; /* 压缩流 */  
  9.     int err;  
  10.   
  11.     /* 这三个字段要在defalteInit之前初始化 */  
  12.     c_stream.zalloc = zalloc;  
  13.     c_stream.zfree = zfree;  
  14.     c_stream.opaque = (voidpf)0;  
  15.   
  16.     /* 初始化压缩流的状态,使用最快速度压缩 */  
  17.     err = deflateInit(&c_stream, Z_BEST_SPEED);  
  18.     CHECK_ERR(err, "deflateInit");  
  19.   
  20.     c_stream.next_out = compr;  
  21.     c_stream.avail_out = (uInt)comprLen;  
  22.   
  23.     /* 这里,uncompr几乎都为0,因此可以很好地被压缩 */  
  24.     c_stream.next_in = uncompr;  
  25.     c_stream.avail_in = (uInt)uncomprLen;  
  26.     err = deflate(&c_stream, Z_NO_FLUSH); /* 压缩输入数据 */  
  27.     CHECK_ERR(err, "deflate");  
  28.     if (c_stream.avail_in != 0) {  
  29.         fprintf(stderr, "deflate not greedy\n");  
  30.         exit(1);  
  31.     }  
  32.   
  33.     /* 把已压缩的数据转换成未压缩: */  
  34.       
  35.     /* 设置流的压缩级别(为未压缩)和压缩策略 */  
  36.     deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);  
  37.     c_stream.next_in = compr;  
  38.     c_stream.avail_in = (uInt)comprLen/2;  
  39.     err = deflate(&c_stream, Z_NO_FLUSH);  
  40.     CHECK_ERR(err, "deflate");  
  41.   
  42.     /* 转换回压缩模式(最高压缩率): */  
  43.     deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);  
  44.     c_stream.next_in = uncompr;  
  45.     c_stream.avail_in = (uInt)uncomprLen;  
  46.     err = deflate(&c_stream, Z_NO_FLUSH);  
  47.     CHECK_ERR(err, "deflate");  
  48.   
  49.     /* 流刷新,产生全部压缩输出 */  
  50.     err = deflate(&c_stream, Z_FINISH);  
  51.     if (err != Z_STREAM_END) {  
  52.         fprintf(stderr, "deflate should report Z_STREAM_END\n");  
  53.         exit(1);  
  54.     }  
  55.     err = deflateEnd(&c_stream);  /* 释放流的资源 */  
  56.     CHECK_ERR(err, "deflateEnd");  
  57. }  
  58.   
  59. /* =========================================================================== 
  60.  * 测试inflate():使用大缓冲区 
  61.  */  
  62. void test_large_inflate(compr, comprLen, uncompr, uncomprLen)  
  63.     Byte *compr, *uncompr;  
  64.     uLong comprLen, uncomprLen;  
  65. {  
  66.     int err;  
  67.     z_stream d_stream; /* 解压流 */  
  68.   
  69.     strcpy((char*)uncompr, "garbage");  
  70.   
  71.     /* 这些个字段要在infalteInit之前初始化 */  
  72.     d_stream.zalloc = zalloc;  
  73.     d_stream.zfree = zfree;  
  74.     d_stream.opaque = (voidpf)0;  
  75.   
  76.     d_stream.next_in  = compr;  
  77.     d_stream.avail_in = (uInt)comprLen;  
  78.   
  79.     /* 初始化解压流 */  
  80.     err = inflateInit(&d_stream);  
  81.     CHECK_ERR(err, "inflateInit");  
  82.   
  83.     /* 解压 */  
  84.     for (;;) {  
  85.         d_stream.next_out = uncompr;            /* 抛弃输出 */  
  86.         d_stream.avail_out = (uInt)uncomprLen;  
  87.         err = inflate(&d_stream, Z_NO_FLUSH);  /* 解压输入数据 */  
  88.         if (err == Z_STREAM_END) break;  
  89.         CHECK_ERR(err, "large inflate");  
  90.     }  
  91.   
  92.     err = inflateEnd(&d_stream);  
  93.     CHECK_ERR(err, "inflateEnd");  
  94.   
  95.     if (d_stream.total_out != 2*uncomprLen + comprLen/2) {  
  96.         fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);  
  97.         exit(1);  
  98.     } else {  
  99.         printf("large_inflate(): OK\n");  
  100.     }  
  101. }  
/* ===========================================================================
 * 测试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:
  1. /* =========================================================================== 
  2.  * 测试deflate():使用完全刷新 
  3.  */  
  4. void test_flush(compr, comprLen)  
  5.     Byte *compr;  
  6.     uLong *comprLen;  
  7. {  
  8.     z_stream c_stream; /* 压缩流 */  
  9.     int err;  
  10.     uInt len = (uInt)strlen(hello)+1;  
  11.   
  12.     c_stream.zalloc = zalloc;  
  13.     c_stream.zfree = zfree;  
  14.     c_stream.opaque = (voidpf)0;  
  15.   
  16.     err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);  
  17.     CHECK_ERR(err, "deflateInit");  
  18.   
  19.     c_stream.next_in  = (Bytef*)hello;  
  20.     c_stream.next_out = compr;  
  21.     c_stream.avail_in = 3;  
  22.     c_stream.avail_out = (uInt)*comprLen;  
  23.     /* 使用完全刷新来压缩字符串 */  
  24.     err = deflate(&c_stream, Z_FULL_FLUSH);  
  25.     CHECK_ERR(err, "deflate");  
  26.   
  27.     compr[3]++; /* 在第一个压缩块中强制产生一个错误 */  
  28.     c_stream.avail_in = len - 3;  
  29.   
  30.     err = deflate(&c_stream, Z_FINISH);  
  31.     if (err != Z_STREAM_END) {  
  32.         CHECK_ERR(err, "deflate");  
  33.     }  
  34.     err = deflateEnd(&c_stream);  
  35.     CHECK_ERR(err, "deflateEnd");  
  36.   
  37.     *comprLen = c_stream.total_out;  
  38. }  
/* ===========================================================================
 * 测试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:
  1. /* =========================================================================== 
  2.  * Test inflateSync() 
  3.  */  
  4. void test_sync(compr, comprLen, uncompr, uncomprLen)  
  5.     Byte *compr, *uncompr;  
  6.     uLong comprLen, uncomprLen;  
  7. {  
  8.     int err;  
  9.     z_stream d_stream; /* 解压流 */  
  10.   
  11.     strcpy((char*)uncompr, "garbage");  
  12.   
  13.     d_stream.zalloc = zalloc;  
  14.     d_stream.zfree = zfree;  
  15.     d_stream.opaque = (voidpf)0;  
  16.   
  17.     d_stream.next_in  = compr;  /* 设置输入缓冲区 */  
  18.     d_stream.avail_in = 2; /* 只读取zlib头部信息 */  
  19.   
  20.     err = inflateInit(&d_stream);  
  21.     CHECK_ERR(err, "inflateInit");  
  22.   
  23.     d_stream.next_out = uncompr;  /* 设置输出缓冲区 */  
  24.     d_stream.avail_out = (uInt)uncomprLen;  
  25.   
  26.     inflate(&d_stream, Z_NO_FLUSH);  
  27.     CHECK_ERR(err, "inflate");  
  28.   
  29.     d_stream.avail_in = (uInt)comprLen-2;   /* 读取所有压缩数据 */  
  30.     err = inflateSync(&d_stream);           /* 但忽略损坏的部分 */  
  31.     CHECK_ERR(err, "inflateSync");  
  32.   
  33.     err = inflate(&d_stream, Z_FINISH);  /* 完成解压 */  
  34.     if (err != Z_DATA_ERROR) {  
  35.         fprintf(stderr, "inflate should report DATA_ERROR\n");  
  36.         /* 因为不正确的adler32 */  
  37.         exit(1);  
  38.     }  
  39.     err = inflateEnd(&d_stream);  
  40.     CHECK_ERR(err, "inflateEnd");  
  41.   
  42.     printf("after inflateSync(): hel%s\n", (char *)uncompr);  
  43. }  
/* ===========================================================================
 * 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):
  1. /* =========================================================================== 
  2.  * 测试:deflate():使用预设的字典 
  3.  */  
  4. void test_dict_deflate(compr, comprLen)  
  5.     Byte *compr;  
  6.     uLong comprLen;  
  7. {  
  8.     z_stream c_stream; /* 压缩流 */  
  9.     int err;  
  10.   
  11.     c_stream.zalloc = zalloc;  
  12.     c_stream.zfree = zfree;  
  13.     c_stream.opaque = (voidpf)0;  
  14.   
  15.     err = deflateInit(&c_stream, Z_BEST_COMPRESSION);  
  16.     CHECK_ERR(err, "deflateInit");  
  17.   
  18.     /* 设置压缩流要使用的字典 */  
  19.     err = deflateSetDictionary(&c_stream,  
  20.                 (const Bytef*)dictionary, (int)sizeof(dictionary));  
  21.     CHECK_ERR(err, "deflateSetDictionary");  
  22.   
  23.     dictId = c_stream.adler;  /* 得到字典的Alder32校验值 */  
  24.     c_stream.next_out = compr;  
  25.     c_stream.avail_out = (uInt)comprLen;  
  26.   
  27.     c_stream.next_in = (Bytef*)hello; /* 输入要压缩的字符串 */  
  28.     c_stream.avail_in = (uInt)strlen(hello)+1;  
  29.   
  30.     /* 直接进行压缩 */  
  31.     err = deflate(&c_stream, Z_FINISH);  
  32.     if (err != Z_STREAM_END) {  
  33.         fprintf(stderr, "deflate should report Z_STREAM_END\n");  
  34.         exit(1);  
  35.     }  
  36.     err = deflateEnd(&c_stream);  
  37.     CHECK_ERR(err, "deflateEnd");  
  38. }  
  39.   
  40. /* =========================================================================== 
  41.  * 测试inflate():使用预设的字典 
  42.  */  
  43. void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)  
  44.     Byte *compr, *uncompr;  
  45.     uLong comprLen, uncomprLen;  
  46. {  
  47.     int err;  
  48.     z_stream d_stream; /* 解压流 */  
  49.   
  50.     strcpy((char*)uncompr, "garbage");  
  51.   
  52.     d_stream.zalloc = zalloc;  
  53.     d_stream.zfree = zfree;  
  54.     d_stream.opaque = (voidpf)0;  
  55.   
  56.     d_stream.next_in  = compr;  
  57.     d_stream.avail_in = (uInt)comprLen;  
  58.   
  59.     err = inflateInit(&d_stream);  
  60.     CHECK_ERR(err, "inflateInit");  
  61.   
  62.     d_stream.next_out = uncompr;  
  63.     d_stream.avail_out = (uInt)uncomprLen;  
  64.   
  65.     for (;;) {  /* 解压 */  
  66.         err = inflate(&d_stream, Z_NO_FLUSH);  
  67.         if (err == Z_STREAM_END) break;  
  68.         if (err == Z_NEED_DICT) {  /* 如果需要字典 */  
  69.             if (d_stream.adler != dictId) {  /* 校验是否与压缩时的字典值一致 */  
  70.                 fprintf(stderr, "unexpected dictionary");  
  71.                 exit(1);  
  72.             }  
  73.             /* 设置解压需要的字典 */  
  74.             err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,  
  75.                                        (int)sizeof(dictionary));  
  76.         }  
  77.         CHECK_ERR(err, "inflate with dict");  
  78.     }  
  79.   
  80.     err = inflateEnd(&d_stream);  
  81.     CHECK_ERR(err, "inflateEnd");  
  82.   
  83.     if (strcmp((char*)uncompr, hello)) {  /* 比较解压后的字符串 */  
  84.         fprintf(stderr, "bad inflate with dict\n");  
  85.         exit(1);  
  86.     } else {  
  87.         printf("inflate with dictionary: %s\n", (char *)uncompr);  
  88.     }  
  89. }  
/* ===========================================================================
 * 测试: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);
    }
}
    下面是命令行程序:
  1. /* =========================================================================== 
  2.  * Usage:  example [output.gz  [input.gz]] 
  3.  */  
  4.   
  5. int main(argc, argv)  
  6.     int argc;  
  7.     char *argv[];  
  8. {  
  9.     Byte *compr, *uncompr;  
  10.     uLong comprLen = 10000*sizeof(int); /* 在MSDOS上不会溢出 */  
  11.     uLong uncomprLen = comprLen;  
  12.     static const char* myVersion = ZLIB_VERSION;  
  13.   
  14.     /* 检查zlib版本是否一致 */  
  15.     if (zlibVersion()[0] != myVersion[0]) {  
  16.         fprintf(stderr, "incompatible zlib version\n");  
  17.         exit(1);  
  18.   
  19.     } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {  
  20.         fprintf(stderr, "warning: different zlib version\n");  
  21.     }  
  22.   
  23.     /* 打印版本和zlib编译信息 */  
  24.     printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",  
  25.             ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());  
  26.   
  27.     /* 分配输入、输出缓冲区的内存 */  
  28.     compr    = (Byte*)calloc((uInt)comprLen, 1);  
  29.     uncompr  = (Byte*)calloc((uInt)uncomprLen, 1);  
  30.     /* 清空compr和uncompr,以避免读到未初始化的数据,并且确保uncompr能很好 
  31.      * 地被压缩 
  32.      */  
  33.     if (compr == Z_NULL || uncompr == Z_NULL) {  
  34.         printf("out of memory\n");  
  35.         exit(1);  
  36.     }  
  37.   
  38.     /* 下面运行各个测试函数 */  
  39. #ifdef Z_SOLO   
  40.     argc = strlen(argv[0]);  
  41. #else   
  42.     test_compress(compr, comprLen, uncompr, uncomprLen);  
  43.   
  44.     test_gzio((argc > 1 ? argv[1] : TESTFILE),  
  45.               uncompr, uncomprLen);  
  46. #endif   
  47.   
  48.     test_deflate(compr, comprLen);  
  49.     test_inflate(compr, comprLen, uncompr, uncomprLen);  
  50.   
  51.     test_large_deflate(compr, comprLen, uncompr, uncomprLen);  
  52.     test_large_inflate(compr, comprLen, uncompr, uncomprLen);  
  53.   
  54.     test_flush(compr, &comprLen);  
  55.     test_sync(compr, comprLen, uncompr, uncomprLen);  
  56.     comprLen = uncomprLen;  
  57.   
  58.     test_dict_deflate(compr, comprLen);  
  59.     test_dict_inflate(compr, comprLen, uncompr, uncomprLen);  
  60.   
  61.     /* 释放缓冲区资源 */  
  62.     free(compr);  
  63.     free(uncompr);  
  64.   
  65.     return 0;  
  66. }  
/* ===========================================================================
 * 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;
}
更多 0
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值