首页 > 编程知识 正文

zlib 压缩文件,zlp压缩文件怎样打开?

时间:2023-05-06 09:36:59 阅读:180346 作者:4003

压缩算法有很多,关于各种算法的开源库也不少,本文将简单介绍zlib压缩算法的基本概念和使用方法。

代码如下所示。

字符串压缩文件(stringstr ) { string strcom; uLong srclen=str.length (; char *buf; ulong buflen=compress bound (srclen ); buf=new char[buflen]; if(compress(Bytef* ) buf,buflen,) bytef* ) str.c_str,srclen )==Z_OK ) strcom.assign ) buf,buf,buf } dbuf 返回字符串; } stringuncompressfile (stringstr ) { string struncom; uLong strlen=str.length (; char *buf; uLong buflen=strlen * 10; buf=new char[buflen]; uncompress (bytef* ) buf,buflen,) bytef * ) str.c_str ),strlen )==Z_OK ) STruncom.asign ) buf } delef } 返回运行; }intmain((/*原始数据)/stringstrsrc ) ' thisisteststring!' 取得); cout 'strsrc is : ' strsrc '; length is : ' strsrc.length () endl; /*压缩后的数据是乱码*/stringstrcom=compress file (strsrc )确认的cout 'strcom is : ' strcom '; length is : ' strcom.length () endl; /*解压缩后的数据必须与元数据匹配。 */stringstruncom=uncompress file (strcom ); cout 'struncom is : ' struncom '; length is : ' struncom.length () endl; 返回0; }在} Linux环境中编译命令:

g -g -W -o test test.cpp -勤奋丝袜输出结果:

(细心的读者可能会注意到,压缩后的字符串反而边变长了。 为什么会这样呢? 要不要换一个长一点的源字符串? ) )

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。