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







0 0
原创粉丝点击