Chinaunix首页 | 论坛 | 博客
  • 博客访问: 812045
  • 博文数量: 77
  • 博客积分: 821
  • 博客等级: 军士长
  • 技术积分: 1904
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-23 16:17
个人简介

学校:上海交通大学软件工程 学历:硕士 行业:从事流媒体移动开发 QQ: 412595942 邮箱:yiikai1987910@gmail.com

文章分类

全部博文(77)

文章存档

2016年(4)

2015年(15)

2014年(16)

2013年(12)

2012年(21)

2011年(9)

分类: C/C++

2014-10-20 13:18:00

    本代码实例为了记录zlib的解压和压缩gzip文件,是最简单的实现。
   类头文件:
   

点击(此处)折叠或打开

  1. #define ZLIB_COPY_BUFFER_SIZE (1024*2)
  2. class vozlib
  3. {
  4. public:
  5.     vozlib();
  6.     ~vozlib();
  7.     VO_GZIP_ERROR_CODE ungzip(VO_PBYTE pInputbuf,VO_U32 inputlen , VO_PBYTE* pOutputbuf,VO_U32 *pOutputlen);
  8.     VO_GZIP_ERROR_CODE gzip(VO_PBYTE pInputbuf,VO_U32 inputlen , VO_PBYTE* pOutputbuf,VO_U32 *pOutputlen);
  9. private:
  10.     VO_VOID init();
  11.     VO_VOID recoverInitBuffer();
  12. protected:
  13.     vozlib(const vozlib& tmp);
  14.     VO_VOID operator=(vozlib& tmp);
  15. private:
  16.     VO_BYTE m_copybuf[ZLIB_COPY_BUFFER_SIZE];
  17.     VO_U32 m_maxoutbufsize;
  18.     VO_PBYTE m_pOutputbuf;

  19. };

    代码实现:
 

点击(此处)折叠或打开

  1. vozlib::vozlib():m_pOutputbuf(NULL),
  2.                  m_maxoutbufsize(0)
  3. {
  4.     init();
  5. }

  6. vozlib::~vozlib()
  7. {
  8.     if(m_pOutputbuf)
  9.     {
  10.         delete[] m_pOutputbuf;
  11.         m_pOutputbuf = NULL;
  12.     }
  13. }

  14. vozlib::vozlib(const vozlib& tmp)
  15. {

  16. }

  17. VO_VOID vozlib::operator=(vozlib& tmp)
  18. {

  19. }


  20. VO_VOID vozlib::init()
  21. {
  22.     memset(m_copybuf,0,sizeof(VO_BYTE)*ZLIB_COPY_BUFFER_SIZE);
  23.     m_pOutputbuf = new VO_BYTE[ZLIB_COPY_BUFFER_SIZE];
  24.     memset(m_pOutputbuf,0,sizeof(VO_BYTE)*ZLIB_COPY_BUFFER_SIZE);
  25.     m_maxoutbufsize = ZLIB_COPY_BUFFER_SIZE;
  26. }

  27. VO_VOID vozlib::recoverInitBuffer()
  28. {
  29.     memset(m_copybuf,0,sizeof(VO_BYTE)*ZLIB_COPY_BUFFER_SIZE);
  30.     if(m_pOutputbuf)
  31.     {
  32.         delete[] m_pOutputbuf;
  33.         m_pOutputbuf = NULL;
  34.     }
  35.     init();
  36. }

  37. VO_GZIP_ERROR_CODE vozlib::gzip(VO_PBYTE pInputbuf,VO_U32 inputlen , VO_PBYTE* pOutputbuf,VO_U32 *pOutputlen)
  38. {
  39.     int ret = 0,have = 0;
  40.     int offset=0;
  41.     z_stream d_stream;
  42.     (*pOutputbuf) = m_pOutputbuf;
  43.     uLong comprLen, uncomprLen;
  44.     uncomprLen =inputlen;
  45.     comprLen =ZLIB_COPY_BUFFER_SIZE;

  46.     strcpy((char*)m_pOutputbuf, "garbage");
  47.     d_stream.zalloc = Z_NULL;
  48.     d_stream.zfree = Z_NULL;
  49.     d_stream.opaque = Z_NULL;
  50.     d_stream.next_in = Z_NULL;
  51.     d_stream.avail_in = 0;

  52.     if(deflateInit2(&d_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
  53.         MAX_WBITS+16, 6, Z_DEFAULT_STRATEGY) != Z_OK)
  54.         return VO_GZIP_INIT_ERROR;
  55.     d_stream.next_in=pInputbuf;
  56.     d_stream.avail_in=uncomprLen;
  57.     do
  58.     {
  59.         d_stream.next_out=m_copybuf;
  60.         d_stream.avail_out=comprLen;
  61.         ret = deflate(&d_stream,Z_FINISH);
  62.         if(ret == Z_STREAM_ERROR)
  63.         {
  64.             return VO_GZIP_STREAM_ERROR;
  65.         }

  66.         switch (ret)
  67.         {
  68.         case Z_NEED_DICT:
  69.             {
  70.                 return VO_GZIP_NEED_DICT;
  71.             }break;
  72.         case Z_DATA_ERROR:
  73.             {
  74.                 (void)deflateEnd(&d_stream);
  75.                 return VO_GZIP_DATA_ERROR;
  76.             }break;
  77.         case Z_MEM_ERROR:
  78.             {
  79.                 (void)deflateEnd(&d_stream);
  80.                 return VO_GZIP_MEMORY_ERROR;
  81.             }break;
  82.         }
  83.         have=comprLen-d_stream.avail_out;
  84.         int    length = offset + have;
  85.         if(length > m_maxoutbufsize)
  86.         {
  87.             m_maxoutbufsize = length + ZLIB_COPY_BUFFER_SIZE;
  88.             VO_PBYTE newoutputbuf = (VO_PBYTE)realloc((*pOutputbuf),sizeof(VO_BYTE) * m_maxoutbufsize);
  89.             if(newoutputbuf != (*pOutputbuf))
  90.             {
  91.                 (*pOutputbuf) = (VO_PBYTE)newoutputbuf;
  92.                 m_pOutputbuf = (*pOutputbuf);
  93.             }
  94.         }
  95.         memcpy((*pOutputbuf)+offset,m_copybuf,have);
  96.         offset+=have;
  97.     }while(d_stream.avail_out==0);
  98.     (void)deflateEnd(&d_stream);
  99.     *pOutputlen = offset/*+sizeof(uLong)*/;
  100.     return VO_GZIP_OK;
  101. }


  102. VO_GZIP_ERROR_CODE vozlib::ungzip(VO_PBYTE pInputbuf,VO_U32 inputlen , VO_PBYTE* pOutputbuf,VO_U32 *pOutputlen)
  103. {
  104.     recoverInitBuffer();
  105.     int ret = 0,have = 0;
  106.     int offset=0;
  107.     z_stream d_stream;
  108.     (*pOutputbuf) = m_pOutputbuf;

  109.     uLong comprLen, uncomprLen;
  110.     comprLen =inputlen;
  111.     uncomprLen =ZLIB_COPY_BUFFER_SIZE;

  112.     strcpy((char*)m_pOutputbuf, "garbage");
  113.     d_stream.zalloc = Z_NULL;
  114.     d_stream.zfree = Z_NULL;
  115.     d_stream.opaque = Z_NULL;
  116.     d_stream.next_in = Z_NULL;
  117.     d_stream.avail_in = 0;
  118.     ret = inflateInit2(&d_stream,47);

  119.     if(ret!=Z_OK)
  120.     {
  121.         return VO_GZIP_INIT_ERROR;
  122.     }

  123.     d_stream.next_in=pInputbuf;
  124.     d_stream.avail_in=comprLen;

  125.     do
  126.     {
  127.         d_stream.next_out=m_copybuf;
  128.         d_stream.avail_out=uncomprLen;
  129.         ret = inflate(&d_stream,Z_NO_FLUSH);
  130.         if(ret == Z_STREAM_ERROR)
  131.         {
  132.             return VO_GZIP_STREAM_ERROR;
  133.         }

  134.         switch (ret)
  135.         {
  136.         case Z_NEED_DICT:
  137.             {
  138.                 return VO_GZIP_NEED_DICT;
  139.             }break;
  140.         case Z_DATA_ERROR:
  141.             {
  142.                 (void)inflateEnd(&d_stream);
  143.                 return VO_GZIP_DATA_ERROR;
  144.             }break;
  145.         case Z_MEM_ERROR:
  146.             {
  147.                 (void)inflateEnd(&d_stream);
  148.                 return VO_GZIP_MEMORY_ERROR;
  149.             }break;
  150.         }
  151.         have=uncomprLen-d_stream.avail_out;
  152.         int    length = offset + have;
  153.         if(length > m_maxoutbufsize)
  154.         {
  155.             m_maxoutbufsize = length + ZLIB_COPY_BUFFER_SIZE;
  156.             VO_PBYTE newoutputbuf = (VO_PBYTE)realloc((*pOutputbuf),sizeof(VO_BYTE) * m_maxoutbufsize);
  157.             if(newoutputbuf != (*pOutputbuf))
  158.             {
  159.                 (*pOutputbuf) = (VO_PBYTE)newoutputbuf;
  160.                 m_pOutputbuf = (*pOutputbuf);
  161.             }
  162.         }
  163.         memcpy((*pOutputbuf)+offset,m_copybuf,have);
  164.         offset+=have;
  165.     }while(d_stream.avail_out==0);

  166.     inflateEnd(&d_stream);
  167.     *pOutputlen = offset;
  168.     return VO_GZIP_OK;

  169. }

阅读(1210) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册