1. #include <stdio.h>
  2. #include <string.h>
  3. #include <assert.h>
  4. #include "zlib.h"
  5.  
  6. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  7. # include <fcntl.h>
  8. # include <io.h>
  9. # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  10. #else
  11. # define SET_BINARY_MODE(file)
  12. #endif
  13.  
  14. #define CHUNK 16384
  15.  
  16. /* Compress from file source to file dest until EOF on source.
  17. def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  18. allocated for processing, Z_STREAM_ERROR if an invalid compression
  19. level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
  20. version of the library linked do not match, or Z_ERRNO if there is
  21. an error reading or writing the files. */
  22. int def(FILE *source, FILE *dest, int level)
  23. {
  24. int ret, flush;
  25. unsigned have;
  26. z_stream strm;
  27. unsigned char in[CHUNK];
  28. unsigned char out[CHUNK];
  29.  
  30. /* allocate deflate state */
  31. strm.zalloc = Z_NULL;
  32. strm.zfree = Z_NULL;
  33. strm.opaque = Z_NULL;
  34. ret = deflateInit(&strm, level);
  35. if (ret != Z_OK)
  36. return ret;
  37.  
  38. /* compress until end of file */
  39. do {
  40. strm.avail_in = fread(in, , CHUNK, source);
  41. if (ferror(source)) {
  42. (void)deflateEnd(&strm);
  43. return Z_ERRNO;
  44. }
  45. flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
  46. strm.next_in = in;
  47.  
  48. /* run deflate() on input until output buffer not full, finish
  49. compression if all of source has been read in */
  50. do {
  51. strm.avail_out = CHUNK;
  52. strm.next_out = out;
  53. ret = deflate(&strm, flush); /* no bad return value */
  54. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  55. have = CHUNK - strm.avail_out;
  56. if (fwrite(out, , have, dest) != have || ferror(dest)) {
  57. (void)deflateEnd(&strm);
  58. return Z_ERRNO;
  59. }
  60. } while (strm.avail_out == );
  61. assert(strm.avail_in == ); /* all input will be used */
  62.  
  63. /* done when last data in file processed */
  64. } while (flush != Z_FINISH);
  65. assert(ret == Z_STREAM_END); /* stream will be complete */
  66.  
  67. /* clean up and return */
  68. (void)deflateEnd(&strm);
  69. return Z_OK;
  70. }
  71.  
  72. /* Decompress from file source to file dest until stream ends or EOF.
  73. inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  74. allocated for processing, Z_DATA_ERROR if the deflate data is
  75. invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
  76. the version of the library linked do not match, or Z_ERRNO if there
  77. is an error reading or writing the files. */
  78. int inf(FILE *source, FILE *dest)
  79. {
  80. int ret;
  81. unsigned have;
  82. z_stream strm;
  83. unsigned char in[CHUNK];
  84. unsigned char out[CHUNK];
  85.  
  86. /* allocate inflate state */
  87. strm.zalloc = Z_NULL;
  88. strm.zfree = Z_NULL;
  89. strm.opaque = Z_NULL;
  90. strm.avail_in = ;
  91. strm.next_in = Z_NULL;
  92. ret = inflateInit(&strm);
  93. if (ret != Z_OK)
  94. return ret;
  95.  
  96. /* decompress until deflate stream ends or end of file */
  97. do {
  98. strm.avail_in = fread(in, , CHUNK, source);
  99. if (ferror(source)) {
  100. (void)inflateEnd(&strm);
  101. return Z_ERRNO;
  102. }
  103. if (strm.avail_in == )
  104. break;
  105. strm.next_in = in;
  106.  
  107. /* run inflate() on input until output buffer not full */
  108. do {
  109. strm.avail_out = CHUNK;
  110. strm.next_out = out;
  111. ret = inflate(&strm, Z_NO_FLUSH);
  112. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  113. switch (ret) {
  114. case Z_NEED_DICT:
  115. ret = Z_DATA_ERROR; /* and fall through */
  116. case Z_DATA_ERROR:
  117. case Z_MEM_ERROR:
  118. (void)inflateEnd(&strm);
  119. return ret;
  120. }
  121. have = CHUNK - strm.avail_out;
  122. if (fwrite(out, , have, dest) != have || ferror(dest)) {
  123. (void)inflateEnd(&strm);
  124. return Z_ERRNO;
  125. }
  126. } while (strm.avail_out == );
  127.  
  128. /* done when inflate() says it's done */
  129. } while (ret != Z_STREAM_END);
  130.  
  131. /* clean up and return */
  132. (void)inflateEnd(&strm);
  133. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  134. }
  135.  
  136. /* report a zlib or i/o error */
  137. void zerr(int ret)
  138. {
  139. fputs("zpipe: ", stderr);
  140. switch (ret) {
  141. case Z_ERRNO:
  142. if (ferror(stdin))
  143. fputs("error reading stdin\n", stderr);
  144. if (ferror(stdout))
  145. fputs("error writing stdout\n", stderr);
  146. break;
  147. case Z_STREAM_ERROR:
  148. fputs("invalid compression level\n", stderr);
  149. break;
  150. case Z_DATA_ERROR:
  151. fputs("invalid or incomplete deflate data\n", stderr);
  152. break;
  153. case Z_MEM_ERROR:
  154. fputs("out of memory\n", stderr);
  155. break;
  156. case Z_VERSION_ERROR:
  157. fputs("zlib version mismatch!\n", stderr);
  158. }
  159. }
  160.  
  161. /* compress or decompress from stdin to stdout */
  162. int main(int argc, char **argv)
  163. {
  164. int ret;
  165.  
  166. /* avoid end-of-line conversions */
  167. SET_BINARY_MODE(stdin);
  168. SET_BINARY_MODE(stdout);
  169.  
  170. /* do compression if no arguments */
  171. if (argc == ) {
  172. ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION);
  173. if (ret != Z_OK)
  174. zerr(ret);
  175. return ret;
  176. }
  177.  
  178. /* do decompression if -d specified */
  179. else if (argc == && strcmp(argv[], "-d") == ) {
  180. ret = inf(stdin, stdout);
  181. if (ret != Z_OK)
  182. zerr(ret);
  183. return ret;
  184. }
  185.  
  186. /* otherwise, report usage */
  187. else {
  188. fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
  189. return ;
  190. }
  191. }

zlib压缩解压示例的更多相关文章

  1. Delphi Base64编码/解码及ZLib压缩/解压

    最近在写的程序与SOAP相关,所以用到了一些Base64编码/解码及数据压缩/解压方面的知识. 在这里来作一些总结:   一.Base64编码/解码   一般用到的是Delphi自带的单元EncdDe ...

  2. java 字符串zlib压缩/解压

    今天在测公司的中间件时发现,增加netty自带的zlib codec压缩处理后,就报decompress failed, invalid head之类的异常.后来发现,直接用bytebuf处理报文体是 ...

  3. SAPCAR 压缩解压软件的使用方法

    SAPCAR 是 SAP 公司使用的压缩解压软件,从 SAP 网站下载的补丁包和小型软件基本都是扩展名为 car 或 sar 的,它们都可以用 SAPCAR 来解压.下面是它的使用说明: 用法: 创建 ...

  4. 使用SevenZipSharp压缩/解压7z格式

    7z格式采用的LZMA算法,号称具有现今最高压缩率.笔者在nuget上搜索7z,在搜索结果中最终选择了SevenZipSharp来进行压缩/解压.不得不说,SevenZipSharp的API设计得非常 ...

  5. WebAPI性能优化之压缩解压

    有时候为了提升WebAPI的性能,减少响应时间,我们会使用压缩和解压,而现在大多数客户端浏览器都提供了内置的解压支持.在WebAPI请求的资源越大时,使用压缩对性能提升的效果越明显,而当请求的资源很小 ...

  6. (转载)C#压缩解压zip 文件

    转载之: C#压缩解压zip 文件 - 大气象 - 博客园http://www.cnblogs.com/greatverve/archive/2011/12/27/csharp-zip.html C# ...

  7. Linux常用命令——压缩解压命令

    Linux常用命令——压缩解压命令 Linux  gzip 描述:压缩文件 语法:gzip [文件名] 压缩后文件格式:.gz gunzip 描述:解压后缀为.gz的文件 语法:gunzip [文件名 ...

  8. 使用C#压缩解压rar和zip格式文件

    为了便于文件在网络中的传输和保存,通常将文件进行压缩操作,常用的压缩格式有rar.zip和7z,本文将介绍在C#中如何对这几种类型的文件进行压缩和解压,并提供一些在C#中解压缩文件的开源库. 在C#. ...

  9. wget下载与tar压缩/解压

    目录 wget命令 下载整个网站 压缩与解压 小节 wget命令 Usage: wget [OPTION]... [URL]... # 后台运行 -b, --background go to back ...

随机推荐

  1. 使用正则表达式匹配JS函数代码

    使用正则表达式匹配JS函数代码 String someFunction="init"; Pattern regex = Pattern.compile("function ...

  2. hadoop-2.6.0.tar.gz + spark-1.5.2-bin-hadoop2.6.tgz 的集群搭建(3节点和5节点皆适用)

    本人呕心沥血所写,经过好一段时间反复锤炼和整理修改.感谢所参考的博友们!同时,欢迎前来查阅赏脸的博友们收藏和转载,附上本人的链接.http://www.cnblogs.com/zlslch/p/584 ...

  3. Sql FAQ

    1.查询结果根据条件翻译成其他值 then '及格' else '不及格' end from S_STUDENT then '及格' else '不及格' end from S_STUDENT 2.s ...

  4. poj1873 The Fortified Forest 凸包+枚举 水题

    /* poj1873 The Fortified Forest 凸包+枚举 水题 用小树林的木头给小树林围一个围墙 每棵树都有价值 求消耗价值最低的做法,输出被砍伐的树的编号和剩余的木料 若砍伐价值相 ...

  5. ASP.NET MVC- Model- An Introduction to Entity Framework for Absolute Beginners

    Introduction This article introduces Entity Framework to absolute beginners. The article is meant fo ...

  6. Javascript 基础知识笔记

    标签(空格分隔): 廖老师学习笔记 javascript 基本入门 根据廖雪峰老师官网,自己看后的简单笔记 第一小节 基本知识 <script type="text/javascrip ...

  7. ios7自带的晃动效果

    ios7自带的晃动效果 by 伍雪颖 - (void)registerEffectForView:(UIView *)aView depth:(CGFloat)depth; { UIInterpola ...

  8. Caused by: java.lang.NoClassDefFoundError: freemarker/cache/TemplateLoader

    1.错误描写叙述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -he ...

  9. sed命令详解及应用实例

    第一部分:Sed基本用法 sed是非交互式的编辑器.它不会修改文件,除非使用shell重定向来保存结果.默认情况下,所有的输出行都被打印到屏幕上. sed编辑器逐行处理文件(或输入),并将结果发送到屏 ...

  10. android中利用实现二级联动的效果

    按照惯例,首先上一张效果图. 本篇文章实现的效果就是如图中所圈的那样,实现类似于HTML中的二级联动的效果. 对于第一个选项我们读取的是本地xml文件来填充数据的, 对于第二个选项我们读取的是通过中央 ...