zoukankan      html  css  js  c++  java
  • zlib的简单使用

    1. 下载zlib库 http://www.zlib.net

    2. 示例源代码

    #include <stdio.h>
    #include <string.h>
    #include "zlib.h"

    #pragma comment(lib, "zlib.lib")

    int main(int argc, char *argv[])
    {
        char buf[1024];
        char destbuf[1024];
        char buf2[1024];

        unsigned long lDestLen = 1024;

        memset(buf, 0, sizeof(buf));
        memset(destbuf, 0, sizeof(destbuf));
        memset(buf2, 0, sizeof(buf2));

        unsigned long srcLen = 64;
        for(int i=0; i<srcLen; ++i)
        {
            buf[i] = i;
        }
        buf [32] = 0;

        int iRet = ::compress((Bytef *)destbuf, &lDestLen, (Bytef *)buf, srcLen);
        if (Z_OK == iRet)
        {
            printf("compress ok dest len %d\n", lDestLen);

            unsigned long lBuf2Len = 1024;
            iRet = ::uncompress((Bytef *)buf2, &lBuf2Len, (Bytef *)destbuf, lDestLen);
            if (Z_OK == iRet)
            {
                printf("uncompress ok dest len %d\n", lBuf2Len);
                if (lBuf2Len != srcLen)
                {
                    printf("uncompress len Different\n");
                }
                else if (memcmp(buf, buf2, lBuf2Len) != 0)
                {
                    printf("uncompress data Different\n");
                }
                else
                {
                    printf("uncompress data same\n");
                }
            }
            else
            {
                printf("uncompress error: %d\n", iRet);
            }
        }
        else
        {
            printf("compress error: %d\n", iRet);
        }

       
        return 0;
    }

    using System.Runtime.InteropServices; 
       
      namespace Server 
      { 
      public enum ZLibError:int 
      { 
      Z_OK  =  0, 
      Z_STREAM_END  =  1, 
      Z_NEED_DICT  =  2, 
      Z_ERRNO  =  (-1), 
      Z_STREAM_ERROR  =  (-2), 
      Z_DATA_ERROR  =  (-3), 
      Z_MEM_ERROR  =  (-4), 
      Z_BUF_ERROR  =  (-5), 
      Z_VERSION_ERROR  =  (-6), 
      } 
       
      public enum ZLibCompressionLevel:int 
      { 
      Z_NO_COMPRESSION  =  0,
      Z_BEST_SPEED  =  1,
      Z_BEST_COMPRESSION  =  9,
      Z_DEFAULT_COMPRESSION  =  (-1)
      } 
       
      public  class  ZLib 
      { 
          [DllImport("zlib.dll")] 
          public static extern ZLibError compress(byte[]dest,ref int destLength,byte[]source,int sourceLength); 
          [DllImport("zlib.dll")] 
          public static extern ZLibError compress2(byte[]dest,ref int destLength,byte[]source,int sourceLength,ZLibCompressionLevel level); 
          [DllImport("zlib.dll")] 
          public static extern ZLibError uncompress(byte[]dest,ref int destLen,byte[]source,intsourceLen);
      } 
      }

  • 相关阅读:
    react 学习
    redux saga学习
    Power BI连接至Amazon Redshift
    php时间日期
    layui select 禁止点击
    微信小程序二维码是无法识别二维码跳转到小程序
    JSON字符串与JSON对象的区别
    前端切图要选择png和jpg呢?
    @media媒体查询
    TortoiseGit revert failed
  • 原文地址:https://www.cnblogs.com/chuncn/p/1506426.html
Copyright © 2011-2022 走看看