zoukankan      html  css  js  c++  java
  • c++ zlib(qt)压缩与解压缩

    #include <QtCore/QCoreApplication>

    #include "zlib.h"
    #include "stdio.h"
    #include <iostream>
    using   namespace   std;
     
    #define MaxBufferSize 999999
     
    void Test1();
    void Test2();
     
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
     
        //Test1();
        Test2();
         
        return a.exec();
    }
     
    //c++ zlib
    void Test1()
    {
        FILE* File_src;
        FILE* File_compress;
        FILE* File_uncompress;
     
        unsigned long len_src;
        unsigned long len_compress;
        unsigned long len_uncompress;
     
        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);
        cout << "normal zlib:" << endl;
        cout << "src: " << buffer_src << ",length:" << len_src << endl << endl;
        cout << "compress: " << buffer_compress << ",length:" << len_compress << endl << endl;
     
        //uncompress
        uncompress(buffer_uncompress,&len_uncompress,buffer_compress,len_compress);
        fwrite(buffer_uncompress,sizeof(char),len_uncompress,File_uncompress);
        cout << "uncompress: " << buffer_uncompress << ",length:" << len_uncompress << endl << endl;
     
        fclose(File_src);
        fclose(File_compress);
        fclose(File_uncompress);
    }
     
    //qt zlib
    void Test2()
    {
        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);
     
        cout << "qt zlib:" << endl;
        cout << "src: " << src.data() << ",length:" << src.size() << endl << endl;
        cout << "compress: " << buffer_compress << ",length:" << len_compress << endl << endl;
        cout << "uncompress: " << buffer_uncompress << ",length:" << len_uncompress << endl << endl;
    }
  • 相关阅读:
    OpenGL中glVertex、显示列表(glCallList)、顶点数组(Vertex array)、VBO及VAO区别
    OpenGL FrameBufferCopy相关Api比较(glCopyPixels,glReadPixels,glCopyTexImage2D,glFramebufferTexture2D)
    OpenGL渲染流程
    GPU大百科全书索引(有助于理解openGL工作流程)
    二进制配置文件为什么比json等配置文件效率高
    游戏开发中的矩阵初探
    Objective-C 30分钟入门教程
    cocos2dx骨骼动画Armature源码分析(三)
    cocos2dx骨骼动画Armature源码分析(二)
    linux中df和du查看磁盘大小不一致解决方法(转载)
  • 原文地址:https://www.cnblogs.com/xumaojun/p/8529519.html
Copyright © 2011-2022 走看看