zlib在Qt C++中的应用
2015-10-12 17:07
393 查看
//testZlib.h
#ifndef TESTZLIB_H
#define TESTZLIB_H
#include "zLib/zlib.h"
#include <QByteArray>
class testZlib
{
public:
testZlib();
public:
int ZlibGetDecodeLength(long InRawLength);
int ZlibGetDecodeLength(QByteArray InRawData);
int ZlibCompress(QByteArray &outEncodeData,QByteArray InRawData,int* nErrorCode = NULL);
int ZlibUncompress(QByteArray &outDecodeData,QByteArray InEncodeData,int* nErrorCode = NULL);
};
#endif // TESTZLIB_H
//testZlib.cpp
#include "testzlib.h"
testZlib::testZlib()
{
}
int testZlib::ZlibGetDecodeLength(long InRawLength)
{
return compressBound(InRawLength);
}
int testZlib::ZlibGetDecodeLength(QByteArray InRawData)
{
return compressBound(InRawData.length());
}
int testZlib::ZlibCompress(QByteArray &outEncodeData,QByteArray InRawData,int * nErrorCode)
{
Bytef *EncodeData =NULL;
int nFunRet = Z_ERRNO;
int nOutLength = 0;
nOutLength = ZlibGetDecodeLength(InRawData.length());
EncodeData = new Byte[nOutLength];
if(EncodeData != NULL)
{
nFunRet = compress(EncodeData,(uLongf*)&nOutLength,(Bytef*)InRawData.data(),InRawData.length());
if(Z_OK == nFunRet)
{
outEncodeData.append((const char*)EncodeData,nOutLength);
}
else
{
nOutLength = -1;
}
delete EncodeData;
}
if(NULL != nErrorCode)
{
*nErrorCode = nFunRet;
}
return nOutLength;
}
int testZlib::ZlibUncompress(QByteArray &outDecodeData,QByteArray InEncodeData,int* nErrorCode)
{
Bytef *DecodeData = NULL;
Bytef *EncodeData_Buffer = NULL;
int nFuncRet = Z_ERRNO;
z_uLongf nOutLength = 0;
nOutLength = ZlibGetDecodeLength(InEncodeData.length());
//for suffer apply of size
DecodeData = new Bytef[nOutLength];
EncodeData_Buffer = new Bytef[nOutLength];
if(DecodeData != NULL && EncodeData_Buffer != NULL)
{
memcpy(EncodeData_Buffer,InEncodeData.constData(),InEncodeData.size());
nFuncRet = uncompress(DecodeData,&nOutLength,(Bytef*)EncodeData_Buffer,InEncodeData.length());
if(Z_OK == nFuncRet)
{
outDecodeData.append((const char*)DecodeData,nOutLength);
}
else
{
nOutLength = -1;
}
}
else
{
nOutLength = -1;
}
if(DecodeData != NULL)
{
delete DecodeData;
}
if(NULL != EncodeData_Buffer)
{
delete EncodeData_Buffer;
}
if(nErrorCode != NULL)
{
*nErrorCode = nFuncRet;
}
return nOutLength;
}
//main测试
int main(int argc, char *argv[])
{
testZlib* test = new testZlib();
QByteArray testByte;
QByteArray testByte2;
int ret = test->ZlibCompress(testByte,QByteArray("helloffworldfffhellof 的确我觉得请问好多哦请问worldffffffffffffff"));
qDebug()<<"compress:"<<ret<<endl;
int ret2 = test->ZlibUncompress(testByte2,testByte);
qDebug()<<"uncompress:"<<ret2<<endl;
}
//参考自:http://blog.csdn.net/sunnysab/article/details/46672949
#ifndef TESTZLIB_H
#define TESTZLIB_H
#include "zLib/zlib.h"
#include <QByteArray>
class testZlib
{
public:
testZlib();
public:
int ZlibGetDecodeLength(long InRawLength);
int ZlibGetDecodeLength(QByteArray InRawData);
int ZlibCompress(QByteArray &outEncodeData,QByteArray InRawData,int* nErrorCode = NULL);
int ZlibUncompress(QByteArray &outDecodeData,QByteArray InEncodeData,int* nErrorCode = NULL);
};
#endif // TESTZLIB_H
//testZlib.cpp
#include "testzlib.h"
testZlib::testZlib()
{
}
int testZlib::ZlibGetDecodeLength(long InRawLength)
{
return compressBound(InRawLength);
}
int testZlib::ZlibGetDecodeLength(QByteArray InRawData)
{
return compressBound(InRawData.length());
}
int testZlib::ZlibCompress(QByteArray &outEncodeData,QByteArray InRawData,int * nErrorCode)
{
Bytef *EncodeData =NULL;
int nFunRet = Z_ERRNO;
int nOutLength = 0;
nOutLength = ZlibGetDecodeLength(InRawData.length());
EncodeData = new Byte[nOutLength];
if(EncodeData != NULL)
{
nFunRet = compress(EncodeData,(uLongf*)&nOutLength,(Bytef*)InRawData.data(),InRawData.length());
if(Z_OK == nFunRet)
{
outEncodeData.append((const char*)EncodeData,nOutLength);
}
else
{
nOutLength = -1;
}
delete EncodeData;
}
if(NULL != nErrorCode)
{
*nErrorCode = nFunRet;
}
return nOutLength;
}
int testZlib::ZlibUncompress(QByteArray &outDecodeData,QByteArray InEncodeData,int* nErrorCode)
{
Bytef *DecodeData = NULL;
Bytef *EncodeData_Buffer = NULL;
int nFuncRet = Z_ERRNO;
z_uLongf nOutLength = 0;
nOutLength = ZlibGetDecodeLength(InEncodeData.length());
//for suffer apply of size
DecodeData = new Bytef[nOutLength];
EncodeData_Buffer = new Bytef[nOutLength];
if(DecodeData != NULL && EncodeData_Buffer != NULL)
{
memcpy(EncodeData_Buffer,InEncodeData.constData(),InEncodeData.size());
nFuncRet = uncompress(DecodeData,&nOutLength,(Bytef*)EncodeData_Buffer,InEncodeData.length());
if(Z_OK == nFuncRet)
{
outDecodeData.append((const char*)DecodeData,nOutLength);
}
else
{
nOutLength = -1;
}
}
else
{
nOutLength = -1;
}
if(DecodeData != NULL)
{
delete DecodeData;
}
if(NULL != EncodeData_Buffer)
{
delete EncodeData_Buffer;
}
if(nErrorCode != NULL)
{
*nErrorCode = nFuncRet;
}
return nOutLength;
}
//main测试
int main(int argc, char *argv[])
{
testZlib* test = new testZlib();
QByteArray testByte;
QByteArray testByte2;
int ret = test->ZlibCompress(testByte,QByteArray("helloffworldfffhellof 的确我觉得请问好多哦请问worldffffffffffffff"));
qDebug()<<"compress:"<<ret<<endl;
int ret2 = test->ZlibUncompress(testByte2,testByte);
qDebug()<<"uncompress:"<<ret2<<endl;
}
//参考自:http://blog.csdn.net/sunnysab/article/details/46672949
相关文章推荐
- 几个C++ 题目
- c++容器概述
- C++ Template 实践草稿(two)
- VS路宏 vc++于OutDir、ProjectDir、SolutionDir不同的路径
- 复杂度1 最大子列和问题 --C语言学习种种
- const的一些常用用法
- C++中string学习
- (转)C/C++ 程序设计员应聘常见 面试笔试 试题深入剖析
- 没有C语言之父,就没有乔布斯
- C++ Builder 利用Rtti访问对象中的字段值
- C# 与C++的数据转换
- C++ 实现通过类名来进行实例化(反射机制?)
- C++连续读入数据
- c++:谁调用了main/WinMain函数!
- C语言中的字符数组与字符串
- 个人记账软件(C++ & mysql)
- (转载)C++之tinyXML使用
- VS2010 使用TeeChart绘图控件
- [Data Mining]APriori算法C++实现
- C++的try_catch异常