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);
原文地址:https://www.cnblogs.com/flysnail/p/2514331.html