zlib在windows上的编译

本文详细介绍了如何使用Zlib库对文件进行压缩和解压缩操作,包括文件大小读取、内存分配、压缩与解压缩过程以及内存释放。

from:

http://blog.sina.com.cn/s/blog_96ea53fb0101ldd9.html

http://blog.youkuaiyun.com/yuzhiyuxia/article/details/25826639

http://blog.youkuaiyun.com/htttw/article/details/7616124

 

首先从http://www.zlib.net/下载了最新的源代码,版本是1.2.8

解压后,实际已经提供了在VC下编译的工程,目录为:
zlib-1.2.8\contrib\vstudio
其中只有vc9和vc10
编译

直接使用其库,下载zlib128-dll,里面有.LIB, .DLL,  .H文件

导入到自己的工程

//解压文件

  CFile bmpFile(_T("C:\\dh_255.bmp"), CFile::modeRead|CFile::typeBinary);
 unsigned long lSize = bmpFile.GetLength();
 CString strSize;
 strSize.Format(_T("%8x"), lSize);  // 文件大小
 unsigned long lSizeDest = lSize;
 if (lSize > 0)
 {
  BYTE* pBufSrc = new BYTE[lSize];
  memset(pBufSrc, 0, lSize);
  BYTE* pBufDest = new BYTE[lSizeDest];
  memset(pBufDest, 0, lSizeDest);
  bmpFile.Read(pBufSrc, lSize);
  
  INT_PTR iComp = compress(pBufDest, &lSizeDest, pBufSrc, lSize);
  CFile zipFile(_T("C:\\dh_255.zip"), CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
  std::string str;
  WCToStr(strSize, str);
  zipFile.Write(str.c_str(), strSize.GetLength() + 1);// 存储解压之后的文件大小
  zipFile.Seek(9+1, CFile::begin);
  zipFile.Write(pBufDest, lSizeDest);
  delete [] pBufDest;
  pBufDest = NULL;
  delete [] pBufSrc;
  pBufSrc = NULL;
  zipFile.Close();
 }
 bmpFile.Close();
 ///* 解压缩 */ 
 CFile zipFile(_T("C:\\dh_255.zip"), CFile::modeRead|CFile::typeBinary);
 unsigned long lZipSrc = zipFile.GetLength();
 unsigned long lZipDest = lSize+ 10;
 BYTE* pBufZip = new BYTE[lZipSrc];
 
 BYTE* pBufSize = new BYTE[9];
 memset(pBufZip, 0, lZipSrc);

 memset(pBufSize, 0, 9);
 zipFile.Read(pBufSize, 9);//////读取 解压之后的文件大小
 unsigned long lSizenew = strtoul((const char*)pBufSize, 0, 16);
 BYTE* pBufBmp = new BYTE[lSizenew];
 memset(pBufBmp, 0, lSizenew);
 zipFile.Seek(9+1, CFile::begin);
 zipFile.Read(pBufZip, lZipSrc);
 
 INT_PTR iUnComp = uncompress(pBufBmp, &lSizenew, pBufZip, lZipSrc);
 CFile destBmpFile(_T("C:\\dh_2550.bmp"), CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
 destBmpFile.Write(pBufBmp, lSizenew);
 delete [] pBufZip;
 pBufZip = NULL;
 zipFile.Close();
 destBmpFile.Close(); 

//压缩数据

 //* 原始数据 */  
 unsigned char strSrc[] = "hello world! aaaaa bbbbb ccccc ddddd 中文测试 yes";  
 unsigned char buf[1024] = {0};  
 unsigned char strDst[1024] = {0};  
 unsigned long srcLen = sizeof(strSrc);  
 unsigned long bufLen = sizeof(buf);  
 unsigned long dstLen = sizeof(strDst); 


 //* 压缩 */  
 compress(buf, &bufLen, strSrc, srcLen); 
 //

 //* 解压缩 */  
 uncompress(strDst, &dstLen, buf, bufLen);
 //* 压缩 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值