点击(此处)折叠或打开

  1. #include <zlib.h>
  2. #include <zconf.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <stdlib.h>
  6. #include <string.h>

  7. int main(int argc,char **argv[])
  8. {
  9.     char msg[1000]="123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n";
  10.     char buf[1000];
  11.     char result[1000];
  12.     unsigned long len, lenresult;

  13.     printf("msg size:%d\n", strlen(msg));
  14.     len = 1000;
  15.     compress(buf, &len, msg, strlen(msg) );
  16.     printf("buf size:%d\n", len);

  17.     lenresult=1000;
  18.     uncompress(result,&lenresult, buf, len );
  19.     
  20.     printf("result size:%d\n", lenresult);
  21.     printf("result:%s\n", result);
  22.     return 0;
  23. }

编译:

LIB = -lz
all : clean tt
.PHONY : all
clean :
    -rm *.o tt
tt : tt.o
    cc --o tt tt.o $(LIB)
tt.: tt.c
    cc --c tt.



程序中需要注意几个地方:

1. 使用compress 与 uncompress之前,必须为目标长度设初值,而且该值要大于你需要解压或者压缩的结果长度,不然会出问题。

2. 编译需要-lz参数。



方式二:   
    gzopen(), gzread(), gzwrite(), gzclose()

09-05 05:53
查看更多