C GZIP得到不正确的结果
当我尝试用C来gzip字符串时,比如C GZIP得到不正确的结果,c,gzip,C,Gzip,当我尝试用C来gzip字符串时,比如 const char *istream = "some me this me"; uLong srcLen = strlen(istream)+1; // +1 for the trailing `\0` uLong destLen = compressBound(srcLen); // this is how you should estimate size
const char *istream = "some me this me";
uLong srcLen = strlen(istream)+1; // +1 for the trailing `\0`
uLong destLen = compressBound(srcLen); // this is how you should estimate size
// needed for the buffer
printf("estimate size needed for the buffer %d\n",destLen);
unsigned char* ostream = (unsigned char*)malloc(destLen);
int res = compress(ostream, &destLen, (const unsigned char *)istream, srcLen);
// destLen is now the size of actuall buffer needed for compression
// you don't want to uncompress whole buffer later, just the used part
printf("actuall buffer needed for compression %d!\n", destLen);
if(res == Z_BUF_ERROR){
printf("Buffer was too small!\n");
return 1;
}
if(res == Z_MEM_ERROR){
printf("Not enough memory for compression!\n");
return 2;
}
printf("ostream %s\n",ostream);
如果输入字符串是像“this is…”、“some me…”“javava…”这样的字符串,那么我得到的输出gzip字符串只有“this”|“some”|“java”。有人知道发生了什么吗?我必须获得完整的gzip字符串,因为我想通过http post bodyeg发送它。输入字符串是“一些我”,输出GZIP字符串将是x\++\\\317MU,在解压缩这个字符串后,我只得到“一些”,但是这里需要得到完整的GZIP字符串…请您的问题而不是在注释中添加信息。C语言和C++语言是不同的语言。你在混合它们吗?提醒,<代码> STD::ISTRAM/
new
而不是malloc
,可能还需要使用std::string
而不是字符数组。gzip压缩的输出是二进制数据,而不是以null结尾的字符串。您不能使用%s
格式打印它。我必须获取完整的gzip字符串,因为我想通过http post bodyeg发送它。输入字符串是“一些我”,输出GZIP字符串将是x\++\\\317MU,在解压缩这个字符串后,我只得到“一些”,但是这里需要得到完整的GZIP字符串…请您的问题而不是在注释中添加信息。C语言和C++语言是不同的语言。你在混合它们吗?提醒,<代码> STD::ISTRAM/new
而不是malloc
,可能还需要使用std::string
而不是字符数组。gzip压缩的输出是二进制数据,而不是以null结尾的字符串。您不能使用%s
格式打印它。