高手速进啊,用zlib中的compress2()压缩后的文件,用uncompress解压时遇到的难题

tianshimeng724 2009-08-15 11:33:31
我用zlib中的compress2()压缩后的文件,用uncompress解压时遇到的问题:
比如:未压缩时文件10k大小,compress2(,,,,1)压缩为9k大小,compress2(,,,,9)压缩为6k大小

现在我要吧压缩后的文件还原,用uncompress(,X,,)第二个参数要我输入解压后的文件大小,源文件大小我没保存,我怎么知道解压后的大小啊。

网上看到用压缩后的文件大小*10作为解压后的大小,但对我不适用

我现在的要求是,不管compress2(,,,,X)等级设为了0-9中任何值,用uncompress(,X,,)解压后都能得到未压缩时文件的大小,请高手帮忙,有其他解决的方法也可,小弟拜谢了
...全文
2172 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
副组长 2009-08-15
  • 打赏
  • 举报
回复
压缩的时候就是写文件,边压边写,写出多少算多少。
解压的时候就是读,读来的压缩数据展开多宽就是多宽。
schlafenhamster 2009-08-15
  • 打赏
  • 举报
回复
which must be large enough to hold the entire uncompressed data.
就是说要足够大,如压缩比可能为4,则buffer大小为压缩文件长度*4(或者10K压缩文件,取固定buffer长度为200k)
socoola 2009-08-15
  • 打赏
  • 举报
回复
zlib的说明:
int uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
Decompresses the source buffer into the destination buffer. sourceLen is the byte length of the source buffer. Upon entry, destLen is the total size of the destination buffer, which must be large enough to hold the entire uncompressed data. (The size of the uncompressed data must have been saved previously by the compressor and transmitted to the decompressor by some mechanism outside the scope of this compression library.) Upon exit, destLen is the actual size of the compressed buffer.
作者都说了,只凭zlib是搞不定的。
一个建议是,你把压缩前的长度和文件信息和压缩后的文件内容合并在一个文件,然后解压的时候就可以先读取文件信息,得到解压之前的长度。
zhoujianhei 2009-08-15
  • 打赏
  • 举报
回复

int inflate(HANDLE hIn, HANDLE hOut)
{
int result = 0;
unsigned char fin[1024], fout[4096];
z_stream d_stream; /* decompression stream */

d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.opaque = (voidpf)0;

d_stream.next_in = fin;
d_stream.next_out = fout;

if(inflateInit(&d_stream) != Z_OK)
return 0;

DWORD read, write;
while(ReadFile(hIn, fin, 1024, &read, 0) && read > 0)
{
d_stream.avail_in = read; d_stream.avail_out = 4096;
result = inflate(&d_stream, Z_NO_FLUSH);
if(result != Z_OK && result != Z_STREAM_END)
return 0;
if(!WriteFile(hOut, fout, 4096-d_stream.avail_out, &write, 0))
return 0;
d_stream.next_in = (Bytef*)fin; d_stream.next_out = fout;
if(result == Z_STREAM_END)
break;
}

if(inflateEnd(&d_stream) != Z_OK)
return 0;

return Z_OK;
}

15,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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