zoukankan      html  css  js  c++  java
  • GZip压缩的实例

    在官网上下载zlib库和源码。

    使用头文件zlib.h

    下面是几个例子,请参考Gzip的Manuals:http://www.gzip.org/zlib/manual.html

      //compress a string
        unsigned char* pOriginal = new unsigned char[1024];
        memset(pOriginal, 0, sizeof(unsigned char)*1024);
        strcpy((char*)pOriginal, "200000000000000000000001000000000000000000002");
    int pOrglen = strlen((char*)pOriginal);
        unsigned char* pCompressed = new unsigned char[1024];
        unsigned long iComLen;
        int iRet = compress(pCompressed, &iComLen, pOriginal, pOrglen);
    
        //uncompress to a string
        unsigned char* pUncompress = new unsigned char[1024];
        memset(pUncompress, 0, 1024);
        unsigned long iUnCLen;
        iRet = uncompress(pUncompress, &iUnCLen, pCompressed, iComLen);
    
        //write into compressed file
        char szFileName[256];
        strcpy(szFileName, "c:\\test.tar.gz");
        void* vp_gzf = gzopen(szFileName, "wb");    
        int written = gzwrite(vp_gzf, pUncompress, iNewUnCLen);
        int result  = gzclose(vp_gzf);
        
        //read from compressed file
        unsigned char* pReadOut = new unsigned char[2048];
        memset(pReadOut, 0, 1024);
        vp_gzf = gzopen(szFileName, "r");
        int ilen = 100;        //the length of the string which uncompressed from file
        int iread = gzread(vp_gzf,pReadOut, ilen);
        result = gzclose(vp_gzf);
  • 相关阅读:
    web.xml中/与/*的区别
    restController相关
    mvc:resources
    RequestMethod 相关
    Springside学习
    命名规范的反思
    C++ 构造中调用构造
    C++ 匿名对象的生命周期
    C++ 构造函数的对象初始化列表
    C++ 类的构造函数使用规则
  • 原文地址:https://www.cnblogs.com/flysnail/p/2514331.html
Copyright © 2011-2022 走看看