Zlib是一种免费且通用的压缩库,由于Zlib压缩效果比LZW好,而且解压缩速度快,更重要的是商业软件中使用Zlib不需要缴纳版权费,所以很多游戏都使用Zlib压缩资源文件。
Zlib是由Jean-loup Gailly 和 Mark Adler共同编写完成的压缩库,Zlib是开源的,而且从Zlib的官方网站http://www.zlib.net/上可以下载到不同平台和编译器下的源代码实现。
Zlib联合使用LZ77算法和Huffman哈夫曼树来实现数据压缩和数据解压。
zlib源码http://download.csdn.net/detail/huangyuancao/7760901
void MainWindow::on_pushButton_clicked() { FILE* File_src; FILE* File_compress; FILE* File_uncompress; unsigned long len_src; unsigned long len_compress; unsigned long len_uncompress = MaxBufferSize; unsigned char *buffer_src = new unsigned char[MaxBufferSize]; unsigned char *buffer_compress = new unsigned char[MaxBufferSize]; unsigned char *buffer_uncompress = new unsigned char[MaxBufferSize]; File_src = fopen("src.txt","r"); File_compress = fopen("compress.txt","w"); File_uncompress = fopen("uncompress.txt","w"); //compress len_src = fread(buffer_src,sizeof(char),MaxBufferSize-1,File_src); compress(buffer_compress,&len_compress,buffer_src,len_src); fwrite(buffer_compress,sizeof(char),len_compress,File_compress); qDebug() << "normal zlib:" ; qDebug() << "src:\n" << buffer_src << ",length:" << len_src ; qDebug() << "compress:\n" << buffer_compress << ",length:" << len_compress ; //uncompress uncompress(buffer_uncompress,&len_uncompress,buffer_compress,len_compress); fwrite(buffer_uncompress,sizeof(char),len_uncompress,File_uncompress); qDebug() << "uncompress:\n" << buffer_uncompress << ",length:" << len_uncompress; fclose(File_src); fclose(File_compress); fclose(File_uncompress); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
void
MainWindow::on_pushButton_2_clicked()
{
QByteArray src;
src.append(
"中华人民共和国,china mobile,123456 "
);
unsigned
long
len_compress;
unsigned
long
len_uncompress;
unsigned
char
*buffer_compress =
new
unsigned
char
[MaxBufferSize];
unsigned
char
*buffer_uncompress =
new
unsigned
char
[MaxBufferSize];
compress(buffer_compress,&len_compress,(Bytef*)src.data(), src.length());
uncompress(buffer_uncompress,&len_uncompress,buffer_compress,len_compress);
qDebug() <<
"qt zlib:"
;
qDebug() <<
"src:\n"
<< src.data() <<
",length:"
<< src.size() ;
qDebug() <<
"compress:\n"
<< buffer_compress <<
",length:"
<< len_compress ;
qDebug() <<
"uncompress:\n"
<< buffer_uncompress <<
",length:"
<< len_uncompress ;
}
|
执行效果:
本文转自夜&枫博客园博客,原文链接:http://www.cnblogs.com/newstart/p/3914888.html,如需转载请自行联系原作者