• Zlib 编译和使用


    1. 由于要弄一个项目,在windows下用到zlib库,现成的库都是release版本的,如果项目是debug的就会导致一大堆符号错误,无奈只得自己重新编译zlib库。

      首先下载 源码和工程文件

      解压到某个目录

      开启VS2010 的命令行 32bit

      进入到zlib-1.2.5contribmasmx86目录

      执行bld_ml32.bat

      打开工程项目zlib-1.2.5contribvstudiovc10zlibvc.sln

      编译整个solution(包括 debug 和 Release)

        此时已经得到了4个文件 zlibwapi.dll zlibwapi.lib zlibwapid.dll zlibwapid.lib 其中 lib 文件是连接期间用到的,dll文件是运行时候用到的。还有两个头文件 zconf.h zlib.h 是在编译期间用到。关于lib 和dll 关系和区别可见链接

        把得到的lib文件放在VS工程目录下,在 属性 -> 连接器 -> 输入 -> 附加依赖项 一栏里面把刚才得到的lib文件加进去,注意末尾带有 d 的表示debug版。

      此时编译,链接依然有错:

      error LNK2019: 无法解析的外部符号 _compress

        在 VS 2010 下通过 dumpbin 命令查看lib导出函数 :

      dumpbin /exports XXX.lib

      可以看到 有compress 的导出。

            ordinal    name
      
                   1    _adler32@12
                 140    _adler32_combine@12
                  39    _compress2@20
                   2    _compress@16

      经过网上查询,发现是要在vs2010的WIN32环境下加入 ZLIB_WINAPI的宏定义才行:

      #define ZLIB_WINAPI
      #include "zconf.h"

      注意这个宏定义要加在 zconf.h 包含之前,如果加载了 zconf.h 之后也不行。 
      此时编译一切顺利。

      参考链接

    2. 下载zlib 
      http://www.zlib.net/ 
      这里写图片描述
    3. 编译zlib库 
      打开sln: zlib-1.2.11contribvstudiovc12zlibvc.sln 
      这里写图片描述
      生成库位置: 
      zlib-1.2.11contribvstudiovc12x86libDllRelease 
      这里写图片描述
    4. 调用zlib库 
      添加lib依赖库位置:zlibwapi.lib的文件位置 
      这里写图片描述 
      添加zlibwapi.lib 在附加依赖库中 键入zlibwapi.lib 
      这里写图片描述 
      添加include位置: 
      这里写图片描述 
      需要两个头文件: 
      zconf.h 
      zlib.h 
      将其copy到一个文件夹中,然后附加到目录即可
    5. 一个例子 
      一个例子:
      char text[] = "zlib compress and uncompress test
    turingo@163.com
    2012-11-05
    ";
      char de_text[1024];
      uLong tlen = strlen(text) + 1;  /* 需要把字符串的结束符''也一并处理 */
      char* buf = NULL;
      uLong blen;
    
      /* 计算缓冲区大小,并为其分配内存 */
      blen = compressBound(tlen); /* 压缩后的长度是不会超过blen的 */
      if ((buf = (char*)malloc(sizeof(char) * blen)) == NULL)
      {
        printf("no enough memory!
    ");
        return -1;
      }
    
      /* 压缩 */
      if (compress((Bytef *)buf, &blen, (Bytef *)text, tlen) != Z_OK)
      {
        printf("compress failed!
    ");
        return -1;
      }
    
      /* 解压缩 */
      if (uncompress((Bytef *)de_text, &tlen, (Bytef *)buf, blen) != Z_OK)
      {
        printf("uncompress failed!
    ");
        return -1;
      }
    
      /* 打印结果,并释放内存 */
      printf("%s", de_text);
      if (buf != NULL)
      {
        free(buf);
        buf = NULL;
      }

    编译 
    一般会出现下面的错误 
    这里写图片描述
    加入宏定义 :ZLIB_WINAPI 
    这里写图片描述

    注意: 
    compress 与uncompress这两个简单接口中的类型转换 
    编码长度和解码长度尽可能保证在256以下,不然容易出错

    官网上的测试代码有些许问题,将其更为256

    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    #include "zlib.h"
    
    
    void MyDoMinus64(LARGE_INTEGER *R,LARGE_INTEGER A,LARGE_INTEGER B)
    {
        R->HighPart = A.HighPart - B.HighPart;
        if (A.LowPart >= B.LowPart)
            R->LowPart = A.LowPart - B.LowPart;
        else
        {
            R->LowPart = A.LowPart - B.LowPart;
            R->HighPart --;
        }
    }
    
    #ifdef _M_X64
    // see http://msdn2.microsoft.com/library/twchhe95(en-us,vs.80).aspx for __rdtsc
    unsigned __int64 __rdtsc(void);
    void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
    {
     //   printf("rdtsc = %I64x
    ",__rdtsc());
       pbeginTime64->QuadPart=__rdtsc();
    }
    
    LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
    {
        LARGE_INTEGER LIres;
        unsigned _int64 res=__rdtsc()-((unsigned _int64)(beginTime64.QuadPart));
        LIres.QuadPart=res;
       // printf("rdtsc = %I64x
    ",__rdtsc());
        return LIres;
    }
    #else
    #ifdef _M_IX86
    void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)
    {
        DWORD dwEdx,dwEax;
        _asm
        {
            rdtsc
            mov dwEax,eax
            mov dwEdx,edx
        }
        pbeginTime64->LowPart=dwEax;
        pbeginTime64->HighPart=dwEdx;
    }
    
    void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
    {
        myGetRDTSC32(pbeginTime64);
    }
    
    LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
    {
        LARGE_INTEGER LIres,endTime64;
        myGetRDTSC32(&endTime64);
    
        LIres.LowPart=LIres.HighPart=0;
        MyDoMinus64(&LIres,endTime64,beginTime64);
        return LIres;
    }
    #else
    void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)
    {
    }
    
    void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
    {
    }
    
    LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
    {
        LARGE_INTEGER lr;
        lr.QuadPart=0;
        return lr;
    }
    #endif
    #endif
    
    void BeginCountPerfCounter(LARGE_INTEGER * pbeginTime64,BOOL fComputeTimeQueryPerf)
    {
        if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(pbeginTime64)))
        {
            pbeginTime64->LowPart = GetTickCount();
            pbeginTime64->HighPart = 0;
        }
    }
    
    DWORD GetMsecSincePerfCounter(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
    {
        LARGE_INTEGER endTime64,ticksPerSecond,ticks;
        DWORDLONG ticksShifted,tickSecShifted;
        DWORD dwLog=16+0;
        DWORD dwRet;
        if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(&endTime64)))
            dwRet = (GetTickCount() - beginTime64.LowPart)*1;
        else
        {
            MyDoMinus64(&ticks,endTime64,beginTime64);
            QueryPerformanceFrequency(&ticksPerSecond);
    
    
            {
                ticksShifted = Int64ShrlMod32(*(DWORDLONG*)&ticks,dwLog);
                tickSecShifted = Int64ShrlMod32(*(DWORDLONG*)&ticksPerSecond,dwLog);
    
            }
    
            dwRet = (DWORD)((((DWORD)ticksShifted)*1000)/(DWORD)(tickSecShifted));
            dwRet *=1;
        }
        return dwRet;
    }
    
    int ReadFileMemory(const char* filename,long* plFileSize,unsigned char** pFilePtr)
    {
        FILE* stream;
        unsigned char* ptr;
        int retVal=1;
        stream=fopen(filename, "rb");
        if (stream==NULL)
            return 0;
    
        fseek(stream,0,SEEK_END);
    
        *plFileSize=ftell(stream);
        fseek(stream,0,SEEK_SET);
        ptr=malloc((*plFileSize)+1);
        if (ptr==NULL)
            retVal=0;
        else
        {
            if (fread(ptr, 1, *plFileSize,stream) != (*plFileSize))
                retVal=0;
        }
        fclose(stream);
        *pFilePtr=ptr;
        return retVal;
    }
    
    int main(int argc, char *argv[])
    {
        int BlockSizeCompress=256;
        int BlockSizeUncompress = 256;//0x8000; 注意需为256
        int cprLevel=Z_DEFAULT_COMPRESSION ;
        long lFileSize;
        unsigned char* FilePtr;
        long lBufferSizeCpr;
        long lBufferSizeUncpr;
        long lCompressedSize=0;
        unsigned char* CprPtr;
        unsigned char* UncprPtr;
        long lSizeCpr,lSizeUncpr;
        DWORD dwGetTick,dwMsecQP;
        LARGE_INTEGER li_qp,li_rdtsc,dwResRdtsc;
    
        if (argc<=1)
        {
            printf("run TestZlib <File> [BlockSizeCompress] [BlockSizeUncompress] [compres. level]
    ");
            return 0;
        }
    
        if (ReadFileMemory(argv[1],&lFileSize,&FilePtr)==0)
        {
            printf("error reading %s
    ",argv[1]);
            return 1;
        }
        else printf("file %s read, %u bytes
    ",argv[1],lFileSize);
    
        if (argc>=3)
            BlockSizeCompress=atol(argv[2]);
    
        if (argc>=4)
            BlockSizeUncompress=atol(argv[3]);
    
        if (argc>=5)
            cprLevel=(int)atol(argv[4]);
    
        lBufferSizeCpr = lFileSize + (lFileSize/0x10) + 0x200;
        lBufferSizeUncpr = lBufferSizeCpr;
    
        CprPtr=(unsigned char*)malloc(lBufferSizeCpr + BlockSizeCompress);
    
        BeginCountPerfCounter(&li_qp,TRUE);
        dwGetTick=GetTickCount();
        BeginCountRdtsc(&li_rdtsc);
        {
            z_stream zcpr;
            int ret=Z_OK;
            long lOrigToDo = lFileSize;
            long lOrigDone = 0;
            int step=0;
            memset(&zcpr,0,sizeof(z_stream));
            deflateInit(&zcpr,cprLevel);
    
            zcpr.next_in = FilePtr;
            zcpr.next_out = CprPtr;
    
    
            do
            {
                long all_read_before = zcpr.total_in;
                zcpr.avail_in = min(lOrigToDo,BlockSizeCompress);
                zcpr.avail_out = BlockSizeCompress;
                ret=deflate(&zcpr,(zcpr.avail_in==lOrigToDo) ? Z_FINISH : Z_SYNC_FLUSH);
                lOrigDone += (zcpr.total_in-all_read_before);
                lOrigToDo -= (zcpr.total_in-all_read_before);
                step++;
            } while (ret==Z_OK);
    
            lSizeCpr=zcpr.total_out;
            deflateEnd(&zcpr);
            dwGetTick=GetTickCount()-dwGetTick;
            dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
            dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
            printf("total compress size = %u, in %u step
    ",lSizeCpr,step);
            printf("time = %u msec = %f sec
    ",dwGetTick,dwGetTick/(double)1000.);
            printf("defcpr time QP = %u msec = %f sec
    ",dwMsecQP,dwMsecQP/(double)1000.);
            printf("defcpr result rdtsc = %I64x
    
    ",dwResRdtsc.QuadPart);
        }
    
        CprPtr=(unsigned char*)realloc(CprPtr,lSizeCpr);
        UncprPtr=(unsigned char*)malloc(lBufferSizeUncpr + BlockSizeUncompress);
    
        BeginCountPerfCounter(&li_qp,TRUE);
        dwGetTick=GetTickCount();
        BeginCountRdtsc(&li_rdtsc);
        {
            z_stream zcpr;
            int ret=Z_OK;
            long lOrigToDo = lSizeCpr;
            long lOrigDone = 0;
            int step=0;
            memset(&zcpr,0,sizeof(z_stream));
            inflateInit(&zcpr);
    
            zcpr.next_in = CprPtr;
            zcpr.next_out = UncprPtr;
    
    
            do
            {
                long all_read_before = zcpr.total_in;
                zcpr.avail_in = min(lOrigToDo,BlockSizeUncompress);
                zcpr.avail_out = BlockSizeUncompress;
                ret=inflate(&zcpr,Z_SYNC_FLUSH);
                lOrigDone += (zcpr.total_in-all_read_before);
                lOrigToDo -= (zcpr.total_in-all_read_before);
                step++;
            } while (ret==Z_OK);
    
            lSizeUncpr=zcpr.total_out;
            inflateEnd(&zcpr);
            dwGetTick=GetTickCount()-dwGetTick;
            dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
            dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
            printf("total uncompress size = %u, in %u step
    ",lSizeUncpr,step);
            printf("time = %u msec = %f sec
    ",dwGetTick,dwGetTick/(double)1000.);
            printf("uncpr  time QP = %u msec = %f sec
    ",dwMsecQP,dwMsecQP/(double)1000.);
            printf("uncpr  result rdtsc = %I64x
    
    ",dwResRdtsc.QuadPart);
        }
    
        if (lSizeUncpr==lFileSize)
        {
            if (memcmp(FilePtr,UncprPtr,lFileSize)==0)
                printf("compare ok
    ");
    
        }
    
        return 0;
    }

    这里,zlib只是把一段内存压缩,压缩后放的另一段内存上,这离压缩文件甚至文件夹的目标还很远

    zlib介绍 
    zlib是提供数据压缩用的函式库,由Jean-loup Gailly与Mark Adler所开发,初版0.9版在1995年5月1日发表。zlib使用DEFLATE算法,最初是为libpng函式库所写的,后来普遍为许多软件所使用。此函式库为自由软件,使用zlib授权 
    参考资料 
    CSDN博客(http://blog.csdn.net/JasonDing1354/article/details/42676685
    http://blog.csdn.net/gubenpeiyuan/article/details/8734290

  • 相关阅读:
    linux的常用命令--pwd命令【持续补充ing】
    linux的常用命令--cp命令【持续补充ing】
    linux的常用命令--mkdir【持续补充ing】
    linux的常用命令--su篇【持续补充ing】
    linux的常用命令--ls篇【持续补充ing】
    linux的常用命令--cd篇【持续补充ing】
    【SQL Server 2008数据库基础篇】常用关键字、数据类型和常用语法
    SpingBoot之多Profile文件
    SpringBoot之YAML
    SpringBoot之HelloWorld仔细分析
  • 原文地址:https://www.cnblogs.com/DeeLMind/p/7243758.html
Copyright © 2020-2023  润新知