【转】C++ ZLib压缩与解压缩

原文:http://www.cnblogs.com/chuncn/archive/2011/03/23/1992481.html
1. #include"stdafx.h"


2. #include"zlib.h"


3. #include<iostream>


4. using namespace std;


5. #pragma comment(lib,"zlib.lib")


6.


7. int_tmain(intargc,_TCHARargv[])


8. {


9. interr;


10. Byte compr[200],uncompr[200];//足够大


11. uLong comprLen,uncomprLen;


12. constchar
hello="1234567890";//原文


13.


14. uLonglen=strlen(hello);//+1;


15. comprLen=sizeof(compr)/sizeof(compr[0]);


16.


17. err=compress(compr,&comprLen,(constBytef)hello,len);


18.


19. if(err!=Z_OK)


20. {


21. cerr<<"压缩错误: "<<err<<'\n';


22. }


23.


24. cout<<"原文长度: "<<len<<"\n压缩后长度:"<<comprLen<<'\n';


25. strcpy((char
)uncompr,"garbage ");


26.


27. err=uncompress(uncompr,&uncomprLen,compr,comprLen);


28.


29. if(err!=Z_OK)


30. {


31. cerr<<"解压错误: "<<err<<'\n';


32. }


33.


34. cout<<"\n原文长度:"<<len<<"\n解压后长度:"<<uncomprLen<<'\n';


35.


36. if(strcmp((char)uncompr,hello))


37. {


38. cerr<<"\n解压失败!!!\n ";


39. }


40. else


41. {


42. cout<<"\n解压成功:"<<(char
)uncompr;


43. }


44.


45. FILE*f=fopen("d:\b1.bin","wb");


46. fwrite(compr,comprLen,1,f);


47. fclose(f);


48.


49. getchar();


50. return 0;


51. }
阅读(3215) | 评论(0) | 转发(0) |0
上一篇:【转】Log4j 配置最全说明

下一篇:【转】程序debug正常release错误
相关热门文章
* 如何解压加上uboot头的ramdisk...
* 欢迎zlibbxh在ChinaUnix博客安...
* rpm包管理
* zabbix php nginx 监控搭建...
* google play store及youtube完...

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/228002

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午1:47
下一篇 2023年2月13日 下午1:47

相关推荐