使用zlib解压时总是得到Z_DATA_ERROR

lgw15046 2012-10-11 04:01:06
有一个文件,大小为2024096字节,该文件的前2323个字节没有经过zlib库压缩,从第2323位后直到文件结尾用zlib压缩,我现在想把从第2323位后直到文件结尾的这些字符解压出来,但是总得到Z_DATA_ERROR,我的代码如下:

int inf(FILE *source, FILE *dest)
{
int ret;
unsigned have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];

/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit(&strm);
// ret = inflateInit2(&strm,8);

if (ret != Z_OK)
return ret;

/* decompress until deflate stream ends or end of file */
do {
strm.avail_in = fread(in, 1, CHUNK, source);
printf("strm.avail_in=%d\n",strm.avail_in);
if (ferror(source)) {
(void)inflateEnd(&strm);
return Z_ERRNO;
}
printf("can go\n");
if (strm.avail_in == 0)
break;
strm.next_in = in;

/* run inflate() on input until output buffer not full */
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
printf("ret=%d\n;Z_NEED_DICT = %d;Z_DATA_ERROR=%d;",ret,Z_NEED_DICT,Z_DATA_ERROR);
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return ret;
}
have = CHUNK - strm.avail_out;
if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
(void)inflateEnd(&strm);
return Z_ERRNO;
}
} while (strm.avail_out == 0);

/* done when inflate() says it's done */
} while (ret != Z_STREAM_END);

/* clean up and return */
(void)inflateEnd(&strm);
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}
void zerr(int ret)
{
printf("zpipe: \n", stderr);
switch (ret) {
case Z_ERRNO:
if (ferror(stdin))
printf("error reading stdin\n", stderr);
if (ferror(stdout))
printf("error writing stdout\n", stderr);
break;
case Z_STREAM_ERROR:
printf("invalid compression level\n", stderr);
break;
case Z_DATA_ERROR:
printf("invalid or incomplete deflate data\n", stderr);
break;
case Z_MEM_ERROR:
printf("out of memory\n", stderr);
break;
case Z_VERSION_ERROR:
printf("zlib version mismatch!\n", stderr);
}
}
int main()
{
 FILE *originalFile = fopen("/root/Desktop/lgw_file_write/folder/jiangbei_original.lg4","a+");
FILE *tempFile = fopen("/root/Desktop/lgw_file_write/folder/1010_change.lg4","a+");
char largeBuf[5000000];
fseek(originalFile,2323,0);
int readLeng =fread(largeBuf,1,2024096-2323,originalFile);
int writeLeng = fwrite(largeBuf,1,readLeng,tempFile);
printf("readLeng = %d;writeLeng =%d\n",readLeng,writeLeng);
if(originalFile != NULL)
{
fclose(originalFile);
originalFile = NULL;
}
if(tempFile != NULL)
{
fclose(tempFile);
tempFile = NULL;
}
FILE *originalFile2 = fopen("/root/Desktop/lgw_file_write/folder/1010_change.lg4","ab+");
FILE *uncompressFile = fopen("/root/Desktop/lgw_file_write/folder/1010_change_uncompress.lg4","ab+");
int infRet = inf(originalFile2,uncompressFile);//使用这个函数解压
printf("the infRet = %d\n",infRet);
if(originalFile2 != NULL)
{
fclose(originalFile2);
originalFile2 = NULL;
}
printf("segment?\n");
if(uncompressFile != NULL)
{
fclose(uncompressFile);
uncompressFile = NULL;
}
}

请教各位使用过zlib压缩库的朋友们,多谢了!
...全文
1726 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bu进ze退 2013-09-27
  • 打赏
  • 举报
回复
可能是参数之间转换时出现的错误,http://www.360doc.com/content/11/1025/11/6979751_159010908.shtml
赵4老师 2013-09-12
  • 打赏
  • 举报
回复
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。 不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
cydenghua 2013-09-12
  • 打赏
  • 举报
回复
哥们, 解决了吗, 我也有这个问题。

24,857

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧