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
- linux C++ 使用zlib 压缩字符串
- python zlib字符串压缩
- zlib的使用-压缩
- [Zlib]_[初级]_[使用zlib库压缩和解压字符串STL string]
- LINUX、C#下使用zlib进行对称压缩、解压
- Android开发论使用Java层的Zlib压缩和使用C底层的Zlib压缩的对比
- 使用zlib压缩IStream流
- 使用zlib压缩IStream流
- Zlib压缩类库使用
- 使用zlib压缩解压缩文件
- 使用zlib压缩/解压文件
- 使用Zlib实现压缩和解压缩
- 使用Zlib实现压缩和解压缩
- 使用Zlib实现压缩和解压缩
- Delphi使用Zlib、C++Builder使用Zlib
- Linux下使用Zlib压缩库,进行文件的压缩与解压缩 (一)
- c++ zlib实现gzip压缩字符串
- [Zlib]_[初级]_[使用zlib库压缩目录]
- Arcgis for javascript实现百度地图ABCD marker的效果
- [android developer 学习笔记]monkey test
- LeetCode 4 双数组找中位数
- 使用adb connect对appium进行无线自动化测试
- wordpress相关路径
- linux C++ 使用zlib 压缩字符串
- java笔记
- SQLServer中DateTime转换成Varchar样式
- Yii2 配置框架国际化支持
- android java 判断是否同一天
- 字符串匹配的KMP算法、Boyer-Moore算法
- js判断浏览器宽度
- <aop:aspectj-autoproxy /> 的用法
- STM32官网寻找固件库的步骤