qt 調用zlib壓縮與解壓縮功能


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);
}
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/chuncn/archive/2011/03/23/1992481.html

qt調用zlib實例http://download.csdn.net/detail/huangyuancao/7760919


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM