加载中…
个人资料
岩岩爸
岩岩爸
  • 博客等级:
  • 博客积分:0
  • 博客访问:53,759
  • 关注人气:9
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

easy zlib 简介

(2011-12-05 09:29:41)
标签:

it

分类: 实用库
最近在找一些简单的zip压缩的库。常用的zlib库一共28个文件,对于小工程来说,实在显得有点大,网上搜了一下,发送easy zlib不错,一共就2个文件,使用也非常方便,推荐给大家使用。


下面贴一些官方的介绍:

easy zlib C/C++ compress and XML compression

This free easy zlib C/C++ source code download makes it painless if you want simple in-memory XML compression (or any text, HTML, image or data compression) and decompression and don't want to spend time learning about zlib.

How to use easy zlib

Download the zip file, open it, copy easyzlib.c and easyzlib.h and add them to your project. Use theezcompress and ezuncompress functions to compress or decompress your source buffer to your destination buffer.


int ezcompress( unsigned char* pDest, long* pnDestLen,
                const unsigned char* pSrc, long nSrcLen );
int ezuncompress( unsigned char* pDest, long* pnDestLen,
                  const unsigned char* pSrc, long nSrcLen );

These are the return codes:

Return code Value Likely reason
EZ_STREAM_ERROR -2 pDest is NULL
EZ_DATA_ERROR -3 corrupted pSrc passed to ezuncompress
EZ_MEM_ERROR -4 out of memory
EZ_BUF_ERROR -5 pDest length is not enough

Determining buffer size for the result

The hard part of using compression functions is guessing or determining buffer sizes. With zlib, you are guaranteed the compressed result will be at most the size of the source plus 12 plus 0.1%. The easyzlib.h header defines EZ_COMPRESSMAXDESTLENGTH to do this calculation for you.


#define EZ_COMPRESSMAXDESTLENGTH(n) (n+(((n)/1000)+1)+12)

That is the maximum memory length of your compressed result. The actual compressed length will be returned from ezcompress in pnDestLen.

If you store the original size with your compressed result, you can allocate exactly the memory you need to decompress it later. However, if you don't know the decompressed size, or you don't want to allocate the maximum compress size, easy zlib provides another option...

Getting the exact result size beforehand

In the core functions ezcompress and ezuncompress, I've added a feature that the regular zlibcompress and uncompress functions don't have. If the destination buffer is too small, the functions still continue on to discover the required memory to complete the operation (which is returned in thepnDestLen parameter).

This way you can pass a small buffer in order to find out the exact size and then allocate only the size you need, whether compressing or decompressing. Here is an example that allocates 8192 bytes up front and then allocates the actual size if 8192 wasn't enough.


long nDestLen = 8192;
unsigned char* pDest = new unsigned char[nDestLen];
int nErr = ezuncompress( pDest, &nDestLen, pSrcBuf, nSrcLen );
if ( nErr == EZ_BUF_ERROR )
{
  delete[] pDest;
  pDest = new unsigned char[nDestLen]; // enough room now
  nErr = ezuncompress( pDest, &nDestLen, pSrcBuf, nSrcLen );
}

Help with allocating buffers

If you are using C++, there are additional overloaded functions implemented in easyzlib.h to make compression even easier. First of all there is a simple buffer class called ezbuffer to streamline your code. By encapsulating the buffer pointer and length in a structure, your code is simpler, plus these functions automatically allocate the destination buffer to accomodate the result.


int ezcompress( ezbuffer& bufDest, const ezbuffer& bufSrc );
int ezuncompress( ezbuffer& bufDest, const ezbuffer& bufSrc );

The resulting destination length is returned in bufDest.nLen. Here are the public members ofezbuffer:


struct ezbuffer
{
  ezbuffer(); // constructor
  ezbuffer( int n ); // constructor allocates n bytes
  ~ezbuffer(); // destructor
  unsigned char* Alloc( int n );
  void Release();
  unsigned char* pBuf;
  long nLen; // data length
};

Here is an example reading nBytes from a file into an ezbuffer and compressing it into anotherezbuffer that is allocated automatically.


ezbuffer bufSrc( nBytes );
fread( bufSrc.pBuf, nBytes, 1, fp );
ezbuffer bufDest;
ezcompress( bufDest, bufSrc );

String and XML compression

C++ string functions are provided if you are using CMarkup and you include easylib.h after Markup.h since the necessary string types and functions are defined in Markup.h. Again, you do not need to estimate the size or do buffer allocations.


int ezcompress( ezbuffer& bufDest, const MCD_STR& strSrc );
int ezuncompress( MCD_STR& strDest, const ezbuffer& bufSrc );

To compress a string such as an XML document, just do the following:


ezbuffer buf;
ezcompress( buf, xml.GetDoc() );

The buf object now contains the compressed document. Use buf.pBuf and buf.nLen to access the compression data.

Why easy zlib?

The zlib compression library is widely used compression software written by Jean-loup Gailly and Mark Adler which is free at zlib.net. If you download it there you will find 28 source code files (plus another 167 supporting files in 34 subfolders).

I've used zlib in numerous projects, and every time I've had to go through the hassle of adding 28 source files to my project. Plus I need to sort out exactly which functions I need, deflate or compress,inflate or uncompress, and which arguments, headers and settings. And there is jargon I have to familiarize myself with everytime I look through zlib.

Also, all of the headers create opportunities for compiling and naming conflicts. There is no reason for every piece of code that needs to compress and uncompress to compile gobs of zlib headers. Headers should provide only the prototypes you need.

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有