免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7571 | 回复: 6
打印 上一主题 下一主题

怎样封装zlib中的压缩解压缩函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-01 10:17 |只看该作者 |倒序浏览
在vc6中,我现在要做一个静态库来封装zlib中的compresst和uncompress函数
我是个新手请大家指教,
最好能给我一份源代码
谢谢

论坛徽章:
0
2 [报告]
发表于 2008-08-01 10:24 |只看该作者
zlib有示例代码的

论坛徽章:
0
3 [报告]
发表于 2008-08-01 11:39 |只看该作者
我知道有示例代码
我看不明白
哪位大哥能帮我封装一下compresst和uncompress

论坛徽章:
0
4 [报告]
发表于 2008-08-01 16:46 |只看该作者

回复 #3 ddbo 的帖子


  1. /* zpipe.c: example of proper use of zlib's inflate() and deflate()
  2.    Not copyrighted -- provided to the public domain
  3.    Version 1.2  9 November 2004  Mark Adler */

  4. /* Version history:
  5.    1.0  30 Oct 2004  First version
  6.    1.1   8 Nov 2004  Add void casting for unused return values
  7.                      Use switch statement for inflate() return values
  8.    1.2   9 Nov 2004  Add assertions to document zlib guarantees
  9. */

  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #include "zlib.h"

  14. #define CHUNK 16384

  15. /* Compress from file source to file dest until EOF on source.
  16.    def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  17.    allocated for processing, Z_STREAM_ERROR if an invalid compression
  18.    level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
  19.    version of the library linked do not match, or Z_ERRNO if there is
  20.    an error reading or writing the files. */
  21. int def(FILE *source, FILE *dest, int level)
  22. {
  23.     int ret, flush;
  24.     unsigned have;
  25.     z_stream strm;
  26.     char in[CHUNK];
  27.     char out[CHUNK];

  28.     /* allocate deflate state */
  29.     strm.zalloc = Z_NULL;
  30.     strm.zfree = Z_NULL;
  31.     strm.opaque = Z_NULL;
  32.     ret = deflateInit(&strm, level);
  33.     if (ret != Z_OK)
  34.         return ret;

  35.     /* compress until end of file */
  36.     do {
  37.         strm.avail_in = fread(in, 1, CHUNK, source);
  38.         if (ferror(source)) {
  39.             (void)deflateEnd(&strm);
  40.             return Z_ERRNO;
  41.         }
  42.         flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
  43.         strm.next_in = in;

  44.         /* run deflate() on input until output buffer not full, finish
  45.            compression if all of source has been read in */
  46.         do {
  47.             strm.avail_out = CHUNK;
  48.             strm.next_out = out;
  49.             ret = deflate(&strm, flush);    /* no bad return value */
  50.             assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
  51.             have = CHUNK - strm.avail_out;
  52.             if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  53.                 (void)deflateEnd(&strm);
  54.                 return Z_ERRNO;
  55.             }
  56.         } while (strm.avail_out == 0);
  57.         assert(strm.avail_in == 0);     /* all input will be used */

  58.         /* done when last data in file processed */
  59.     } while (flush != Z_FINISH);
  60.     assert(ret == Z_STREAM_END);        /* stream will be complete */

  61.     /* clean up and return */
  62.     (void)deflateEnd(&strm);
  63.     return Z_OK;
  64. }

  65. /* Decompress from file source to file dest until stream ends or EOF.
  66.    inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  67.    allocated for processing, Z_DATA_ERROR if the deflate data is
  68.    invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
  69.    the version of the library linked do not match, or Z_ERRNO if there
  70.    is an error reading or writing the files. */
  71. int inf(FILE *source, FILE *dest)
  72. {
  73.     int ret;
  74.     unsigned have;
  75.     z_stream strm;
  76.     char in[CHUNK];
  77.     char out[CHUNK];

  78.     /* allocate inflate state */
  79.     strm.zalloc = Z_NULL;
  80.     strm.zfree = Z_NULL;
  81.     strm.opaque = Z_NULL;
  82.     strm.avail_in = 0;
  83.     strm.next_in = Z_NULL;
  84.     ret = inflateInit(&strm);
  85.     if (ret != Z_OK)
  86.         return ret;

  87.     /* decompress until deflate stream ends or end of file */
  88.     do {
  89.         strm.avail_in = fread(in, 1, CHUNK, source);
  90.         if (ferror(source)) {
  91.             (void)inflateEnd(&strm);
  92.             return Z_ERRNO;
  93.         }
  94.         if (strm.avail_in == 0)
  95.             break;
  96.         strm.next_in = in;

  97.         /* run inflate() on input until output buffer not full */
  98.         do {
  99.             strm.avail_out = CHUNK;
  100.             strm.next_out = out;
  101.             ret = inflate(&strm, Z_NO_FLUSH);
  102.             assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
  103.             switch (ret) {
  104.             case Z_NEED_DICT:
  105.                 ret = Z_DATA_ERROR;     /* and fall through */
  106.             case Z_DATA_ERROR:
  107.             case Z_MEM_ERROR:
  108.                 (void)inflateEnd(&strm);
  109.                 return ret;
  110.             }
  111.             have = CHUNK - strm.avail_out;
  112.             if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  113.                 (void)inflateEnd(&strm);
  114.                 return Z_ERRNO;
  115.             }
  116.         } while (strm.avail_out == 0);
  117.         assert(strm.avail_in == 0);     /* all input will be used */

  118.         /* done when inflate() says it's done */
  119.     } while (ret != Z_STREAM_END);

  120.     /* clean up and return */
  121.     (void)inflateEnd(&strm);
  122.     return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  123. }

  124. /* report a zlib or i/o error */
  125. void zerr(int ret)
  126. {
  127.     fputs("zpipe: ", stderr);
  128.     switch (ret) {
  129.     case Z_ERRNO:
  130.         if (ferror(stdin))
  131.             fputs("error reading stdin\n", stderr);
  132.         if (ferror(stdout))
  133.             fputs("error writing stdout\n", stderr);
  134.         break;
  135.     case Z_STREAM_ERROR:
  136.         fputs("invalid compression level\n", stderr);
  137.         break;
  138.     case Z_DATA_ERROR:
  139.         fputs("invalid or incomplete deflate data\n", stderr);
  140.         break;
  141.     case Z_MEM_ERROR:
  142.         fputs("out of memory\n", stderr);
  143.         break;
  144.     case Z_VERSION_ERROR:
  145.         fputs("zlib version mismatch!\n", stderr);
  146.     }
  147. }

  148. /* compress or decompress from stdin to stdout */
  149. int main(int argc, char **argv)
  150. {
  151.     int ret;

  152.     /* do compression if no arguments */
  153.     if (argc == 1) {
  154.         ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION);
  155.         if (ret != Z_OK)
  156.             zerr(ret);
  157.         return ret;
  158.     }

  159.     /* do decompression if -d specified */
  160.     else if (argc == 2 && strcmp(argv[1], "-d") == 0) {
  161.         ret = inf(stdin, stdout);
  162.         if (ret != Z_OK)
  163.             zerr(ret);
  164.         return ret;
  165.     }

  166.     /* otherwise, report usage */
  167.     else {
  168.         fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
  169.         return 1;
  170.     }
  171. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2008-08-01 17:06 |只看该作者
-_-!

伸手党

论坛徽章:
0
6 [报告]
发表于 2008-08-01 17:08 |只看该作者
原帖由 ddbo 于 2008-8-1 11:39 发表
我知道有示例代码
我看不明白
哪位大哥能帮我封装一下compresst和uncompress


自己动手,丰衣足食。

论坛徽章:
0
7 [报告]
发表于 2008-08-01 17:30 |只看该作者
原帖由 ddbo 于 2008-8-1 11:39 发表
我知道有示例代码
我看不明白
哪位大哥能帮我封装一下compresst和uncompress

看不懂算法实现 接口应该可以看懂吧 接口看懂不就可以封装了吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP