不废话
#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:
};