zlib安装,使用c++

linux下Zlib的安装与使用

https://blog.csdn.net/qq_21383435/article/details/79539034

http://www.zlib.net/

https://github.com/gdsgtcz/Zlib-Lua-C/blob/master/lua-zlib/ZLibString.cpp

 

#include "ZLibString.h"

CZlibMgr* CZlibMgr::instance = NULL;

CZlibMgr::CZlibMgr()
{
}

CZlibMgr::~CZlibMgr()
{
}

CZlibMgr* CZlibMgr::getInstance()
{
    if (instance == NULL) {
        instance = new CZlibMgr();
    }
    return instance;
}

bool CZlibMgr::Compress(const char* pcContentBuf, char* pcCompBuf, unsigned long& ulCompLen)
{
    if (pcContentBuf == NULL)
    {
        return false;
    }
    
    if (strlen(pcContentBuf) == 0)
    {
        return false;
    }
    
    memset(compr, 0, MAXBUFFERSIZE);
    
    uLong comprLen;
    int err;
    
    uLong len = strlen(pcContentBuf);
    comprLen = sizeof(compr) / sizeof(compr[0]);
    
    err = compress(compr, &comprLen, (const Bytef*)pcContentBuf, len);
    if (err != Z_OK)
    {
        cout << "compess error: " << err << endl;
        return false;
    }
    cout << "orignal size: " << len << " , compressed size : " << comprLen << endl;
    memcpy(pcCompBuf, compr, comprLen);
    ulCompLen = comprLen;
    
    return true;
}

bool CZlibMgr::UnCompress(const char* pcCompBuf, char* pcUnCompBuf, unsigned long ulCompLen)
{
    if (pcCompBuf == NULL)
    {
        cout <<__FUNCTION__ << "================> pcCompBuf is null please to check " << endl;
        return false;
    }
    
    if (strlen(pcCompBuf) == 0)
    {
        cout <<__FUNCTION__ << "strlen(pcCompBuf) == 0  ========================> " << endl;
        return false;
    }
    
    memset(uncompr, 0, MAXBUFFERSIZE);
    uLong uncomprLen = MAXBUFFERSIZE;
    int err;
    
    err = uncompress(uncompr, &uncomprLen, (const Bytef *)pcCompBuf, ulCompLen);
    if (err != Z_OK)
    {
        cout << "uncompess error: " << err << endl;
        return false;
    }
    
    cout << "compress size: " << ulCompLen << "  uncompressed size : " << uncomprLen << endl;
    memcpy(pcUnCompBuf, uncompr, uncomprLen);
    
    return true;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值