zlib库使用例子

不废话

#define ZLIB_COMPRESS_DEFAULT_MAX_LEN (100 * 1024 * 1024)//解压后的包不能大于ZLIB_COMPRESS_DEFAULT_MAX_LEN
class zlibOptClass
{
public:
    zlibOptClass()
    {
    }
    virtual ~zlibOptClass()
    {
    }
    static int compress(string &in, string &out, int compressMaxLen = ZLIB_COMPRESS_DEFAULT_MAX_LEN, int level = 9)
    {
        uLong tmpCompressLen;
        int comperr;
        std::string tmpstr;
        if ((int)in.length() >= compressMaxLen)
        {
            logwerr("err: input len = %d, max set len = %d", (int)in.length(), compressMaxLen);
            return -2;
        }
        tmpCompressLen = compressBound(in.length());
        tmpstr.resize(tmpCompressLen);
        // comperr = compress((Bytef *)(&tmpstr[0]), &tmpCompressLen, (const Bytef *)in.c_str(), in.length(), level);
        comperr = compress2((Bytef *)(&tmpstr[0]), &tmpCompressLen, (const Bytef *)in.c_str(), in.length(), level);
        if (comperr != Z_OK)
        {
            logwerr("err:%d", comperr);
            out.clear();
            return -1;
        }
        out = tmpstr.substr(0, tmpCompressLen);
        return tmpCompressLen;
    }

    static int uncompress(string &in, string &out, uLong compressMaxLen = ZLIB_COMPRESS_DEFAULT_MAX_LEN)
    {
        int comperr;
        std::string tmpstr;
        uLong inlen = in.length();
        tmpstr.resize(compressMaxLen);
        //comperr = uncompress(tmpuncomp, &tmpuncompLen, (const Bytef*)in.c_str(), in.length());
        comperr = uncompress2((Bytef *)(&tmpstr[0]), &compressMaxLen, (const Bytef *)in.c_str(), &inlen);
        if (comperr != Z_OK)
        {
            logwerr("err:%d", comperr);
            out.clear();
            return -1;
        }
        out = tmpstr.substr(0, compressMaxLen);
        return compressMaxLen;
    }

    static int uncompress(const u8 *buf, int buflen, string &out, uLong compressMaxLen = ZLIB_COMPRESS_DEFAULT_MAX_LEN)
    {
        int comperr;
        std::string tmpstr;
        uLong inlen = buflen;
        tmpstr.resize(compressMaxLen);
        comperr = uncompress2((Bytef *)(&tmpstr[0]), &compressMaxLen, (const Bytef *)buf, &inlen);
        if (comperr != Z_OK)
        {
            logwerr("err:%d", comperr);
            out.clear();
            return -1;
        }
        out = tmpstr.substr(0, compressMaxLen);
        return compressMaxLen;
    }
private:
};


#define ZLIB_OPERATE_MAX_SIZE (10 * 1024 * 1024) // 解压后的包不能大于ZLIB_COMPRESS_DEFAULT_MAX_LEN
#define ZLIB_OPERATE_DEFAULT_SIZE (512 * 1024) // 解压后的包不能大于ZLIB_COMPRESS_DEFAULT_MAX_LEN
class zlibOptClassV1
{
public:
    zlibOptClassV1()
    {
    }
    virtual ~zlibOptClassV1()
    {
    }
    static int compress(std::string &instr, std::string &outstr, int compressMaxLen = ZLIB_COMPRESS_DEFAULT_MAX_LEN, int level = 9)
    {
        uLong tmpCompressLen;
        int errorflag;
        if ((int)instr.length() >= compressMaxLen)
        {
            logwerr("err: input len = %d, max set len = %d", (int)instr.length(), compressMaxLen);
            return -2;
        }
        tmpCompressLen = compressBound(instr.length());
        outstr.resize(tmpCompressLen);
        errorflag = compress2((Bytef *)(&outstr[0]), &tmpCompressLen, (const Bytef *)instr.c_str(), instr.length(), level);
        if (errorflag != Z_OK)
        {
            logwerr("err:%d", errorflag);
            outstr.clear();
            return -1;
        }
        outstr.resize(tmpCompressLen);
        return tmpCompressLen;
    }

    static int uncompress(std::string &instr, std::string &outstr, int maxoutsize = ZLIB_OPERATE_MAX_SIZE)
    {
        int errorflag;
        uLong inlen;
        uLong outsize;

        if(instr.length() < ZLIB_OPERATE_DEFAULT_SIZE)
        {
            inlen = instr.length();
            outsize = ZLIB_OPERATE_DEFAULT_SIZE;
            outstr.resize(outsize);
            errorflag = uncompress2((Bytef *)(&outstr[0]), &outsize, (const Bytef *)instr.c_str(), &inlen);
            if (errorflag == Z_OK)
            {
                outstr.resize(outsize);
                return outsize;
            }
        }

        inlen = instr.length();
        outsize = maxoutsize;
        outstr.resize(maxoutsize);
        errorflag = uncompress2((Bytef *)(&outstr[0]), &outsize, (const Bytef *)instr.c_str(), &inlen);
        if (errorflag == Z_OK)
        {
            outstr.resize(outsize);
            return outsize;
        }
        logwdbg("%d", errorflag);
        return -1;
    }

    static int uncompress(const u8 *buf, int buflen, std::string &outstr, int maxoutsize = ZLIB_OPERATE_MAX_SIZE)
    {
        int errorflag;
        uLong inlen;
        uLong outsize;

        if(buflen < ZLIB_OPERATE_DEFAULT_SIZE)
        {
            inlen = buflen;
            outsize = ZLIB_OPERATE_DEFAULT_SIZE;
            outstr.resize(outsize);
            errorflag = uncompress2((Bytef *)(&outstr[0]), &outsize, (const Bytef *)buf, &inlen);
            if (errorflag == Z_OK)
            {
                outstr.resize(outsize);
                return outsize;
            }
        }

        inlen = buflen;
        outsize = maxoutsize;
        outstr.resize(maxoutsize);
        errorflag = uncompress2((Bytef *)(&outstr[0]), &outsize, (const Bytef *)buf, &inlen);
        if (errorflag == Z_OK)
        {
            outstr.resize(outsize);
            return outsize;
        }
        logwdbg("%d", errorflag);
        return -1;
    }

private:
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值