linux C++ 使用zlib 压缩字符串

$wget http://www.zlib.net/zlib-1.2.3.tar.gz
$tar -xvzf  zlib-1.2.3.tar.gz
$cd zlib-1.2.3.tar.gz
$./configure
$make
$sudo make install

安装好之后,就可以用了

zlibmgr.h 文件

#ifndef _ZLIBMGR
#define _ZLIBMGR
#define  MAXBUFFERSIZE 200000
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <zlib.h>

using namespace std;

class CZlibMgr
{
public:
    CZlibMgr();
    ~CZlibMgr();

    bool Compress(const char* pcContentBuf, char* pcCompBuf, unsigned long& ulCompLen);  // 压缩,pcContentBuf 要压缩的内容 pcCompBuf 压缩后的内容 ulCompLen 压缩后的长度
    bool UnCompress(const char* pcCompBuf, char* pcUnCompBuf, unsigned long ulCompLen); // 解压,pcCompBuf 压缩的内容, pcUnCompBuf 解压后的内容  ulCompLen 压缩内容的长度

private:
    Byte compr[MAXBUFFERSIZE];
    Byte uncompr[MAXBUFFERSIZE];    

};

CZlibMgr::CZlibMgr()
{
}

CZlibMgr::~CZlibMgr()
{
}

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;
}

CZlibMgr g_kZlibMgr;

#endif

使用的例子

#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <zlib.h>
#include "zlibmgr.h"
using namespace std;
extern CZlibMgr g_kZlibMgr;

int main()
{
    cout << sizeof(long) << "  " << sizeof(int) << endl;
    long uu1 = 10000;
    unsigned int i1 = 0;
    i1 = (unsigned int)uu1;
    cout << i1 << endl;
    int err;
    Byte compr[200000], uncompr[200000];    // big enough
    memset(compr, 0, 200000);
    memset(uncompr, 0, 200000);

    uLong comprLen, uncomprLen;
    const char* hello = "guosyidlsldkksldkieeeeeeeee211111111111111111111111111111111111\"";

    unsigned long u1, u2;
    u1 = 0;
    u2 = 0;
    char sOutBuf[8096];
    g_kZlibMgr.Compress(hello, sOutBuf, u1);
    cout << "======================> " << u1 << endl;

    char sUnCompressBuf[8096];
    memset(sUnCompressBuf, 0, sizeof(sUnCompressBuf));
    g_kZlibMgr.UnCompress(sOutBuf, sUnCompressBuf, u1);
    cout << sUnCompressBuf << endl;
}

采用如下方式进行编译
g++ -o testzlib testzlib.cpp -lz

大家可以把zlibmgr 直接拿过去用,压缩效果很好 通常都在一半以上

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值