您的位置:首页 > 编程语言 > C语言/C++

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: