zlib在Qt C++中的应用
来源:互联网 发布:环保部网络举报 编辑:程序博客网 时间:2024/06/07 01:13
//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
- zlib在Qt C++中的应用
- 在C++BUILDER中应用ZLIB
- 在C++BUILDER中应用ZLIB
- Qdebug在qt调试中的应用
- C++ 11 在 Qt 5 中的应用
- SQlite在c#中的应用
- zlib库在windows系统中的编译
- Java在C/S网络中的应用
- 二分法在C#.net 中的应用
- 结构在C程序中的应用
- 数据结构在C语言中的应用
- static在C语言中的应用
- CONST在C语言中的应用
- 理解ARC在Objective-C中的应用
- 理解ARC在Objective-C中的应用
- 理解ARC在Objective-C中的应用
- 理解ARC在Objective-C中的应用
- extern 在C语言中的应用
- vector容器1(添加元素,遍历元素)
- 17.1.2.3 Determination of Safe and Unsafe Statements in Binary Logging
- 第六周 项目1 建立顺序栈算法库
- 系统学习asp.net Ajax中GridView、UpdatePanel、UpdateProgress、Timer使用
- MyBatis的动态SQL详解
- zlib在Qt C++中的应用
- java自带线程池和队列详细讲解
- 如何修改WAMP中mysql默认空密码&重新登录phpmyadmin
- 第七周项目(2):建立链队算法库
- 进程的用户栈和内核栈
- java byte b = 0x80;
- 第7周、项目1—建立顺序环形队列算法库
- 项目2 -- 建立链栈算法库
- 第七周项目二建立链队算法库