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

来源:互联网 发布:咖啡网络课程 编辑:程序博客网 时间:2024/06/06 05:46
$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 0
原创粉丝点击