请问用过Zlib库的兄弟施手相助! 请问为何我用Zlib库来压缩一段数据(用compress()函数),为何第一个次的返回值为正常的:0(Z_OK),再用返回值就是-5(Z_BUF_ERROR)了.谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //Start FILE * fpSource; FILE * fpTarget; DWORD dwSourceLen; DWORD dwTargetLen; int iSourceLen; int iReadTimes; int iLeaveBytes; BYTE bSource[1024]; BYTE bTarget[2048]; int iCompRet = -1; if(!(fpSource = fopen("019.bmp","rb"))) { AfxMessageBox("Open Source File Error!"); return; } fseek(fpSource,0,SEEK_SET); fseek(fpSource,0,SEEK_END); iSourceLen = ftell(fpSource); iReadTimes = iSourceLen / 1024; iLeaveBytes = iSourceLen - iReadTimes * 1024; if(!(fpTarget = fopen("019bak.bmp","wb"))) { AfxMessageBox("Create Target File Error!"); return; } dwSourceLen = 1024 ; dwTargetLen = 2048 ; fseek(fpSource,0,SEEK_SET); for(int i = 0 ; i < iReadTimes ; i++) { iCompRet = -1 ; memset(bSource,0,1024); memset(bTarget,0,2048); fread(bSource,1024,1,fpSource); iCompRet = compress(bTarget,&dwTargetLen,bSource,dwSourceLen); fwrite(bTarget,dwTargetLen,1,fpTarget); } memset(bSource,0,1024); memset(bTarget,0,2048); dwSourceLen = dwTargetLen = iLeaveBytes ; fread(bSource,iLeaveBytes,1,fpSource); iCompRet = compress(bTarget,&dwTargetLen,bSource,dwSourceLen); fwrite(bTarget,dwTargetLen,1,fpTarget); if(fpSource) fclose(fpSource); if(fpTarget) fclose(fpTarget);//End压出来的结果与长度严重不对! 帮你up...顺便问一下,我有个zlib例子里面,压缩前和解压缩后的数据为什么会不一样?是对操作系统有要求吗?------------------------::高级编程小书童:: dwTargetLen = 2048; for(int i = 0 ; i < iReadTimes ; i++) { iCompRet = -1 ; memset(bSource,0,1024); memset(bTarget,0,2048); fread(bSource,1024,1,fpSource); iCompRet = compress(bTarget,&dwTargetLen,bSource,dwSourceLen); fwrite(bTarget,dwTargetLen,1,fpTarget); }我果然说得不错,问题就在上面,注意到compress中dwTargetLen是一个in\out参数,就是说它的输入也是有意义的,输出也是有意义的,你第一次运行的时候dwTargetlen 为2048,压缩第一次以后这个值假设为1000,但是第二次压缩compress就会认为你的目标缓冲就是1000个字节,如果它第二次压缩以后要求的长度超过1000就会出现你给出的那个错误。你应该在每次压缩前重新设置dwTargetLen的值为2048你的程序写得太不好了,我竟然看到memset(xxx, 0, 1024);这是很不好的风格,好的风格应该是这样memset(xxx, 0, sizeof(xxx));这样几十xxx的定义修改为char xxx[100];也不会有问题,要照你那样写就肯定有问题。另外你读文件的时候也有问题,fread(bSource,1024,1,fpSource);你这样读如果读出的数据够1024,那么返回为1,而不是1024,你应该这样写fread(bSource, 1, 1024, fpSource); http://www.csdn.net/expert/topic/1043/1043012.xml?temp=2.820987E-02 to harry202(harry) 当然不一样了…… 多谢Oldworm的批评.我会好好努力的. Oldworm 如果没有问题,他来这里问什么?如果你能决绝你写代码。何必在这里说那么许多废话! /*本来要下去睡觉……我最讨厌评论员文章!*/#include "stdafx.h"#include "zlib.h"#include <string.h>int main(int argc, char* argv[]){ const Bytef d[]="恶心恶心恶心恶心恶心恶心恶心恶心"; printf("%s\n", d); Bytef* o=new Bytef[75]; uLong e=75L; int result = compress(o, &e, d, strlen((const char*)d)); if( Z_OK == result ) { printf("压缩尺寸:%d\n", e); } else { printf("错误!\n"); } delete[] o; return 0;} SWF to CWS 他的压缩方式是分块 压缩 头压缩 其他部分也压缩 本身SWF也采用了Zlib压缩了!如果你用全不文件压缩 那当然只有头符合要求了! 其他部分 你只是 做了 重复压缩的工作明白了吗????!!!!看来你的问题不在ZLIB上 而是在CWS压缩 格式 和分块上!你现在研究的问题 已经可以脱离 Zlib了! 头已经符合了…… 那说明你Zlib已经压缩正常了…… 没有任何问题了! 哪位有Zlib的例子,可以发给我一份吗,我的总是编译出错 error LNK2001: unresolved external symbol _compress[email protected] 如何在ActiveX控件上放置MFC界面元素? 进程间的Event互斥使用 代码自动生成 无法用Post方法调用WebService 鼠标左键被按下时,EditBox的显示内容为何无法更新? [求助]如何让两个按钮控件都保持按下状态! Cricheditview中为什么没有onchang()消息映射函数呢?用于检测是不是输入了汉字? 有关无法写入注册表的问题。。不知道该发到哪个区 DialogBar上的控件如编辑框里的值如何更新?100分悬赏 请问关于WINSOCK编程的资源回收问题 關與Opaint()刷新的問題,是概念性的! 网卡问题
FILE * fpSource;
FILE * fpTarget;
DWORD dwSourceLen;
DWORD dwTargetLen;
int iSourceLen;
int iReadTimes;
int iLeaveBytes;
BYTE bSource[1024];
BYTE bTarget[2048];
int iCompRet = -1; if(!(fpSource = fopen("019.bmp","rb")))
{
AfxMessageBox("Open Source File Error!");
return;
}
fseek(fpSource,0,SEEK_SET);
fseek(fpSource,0,SEEK_END);
iSourceLen = ftell(fpSource);
iReadTimes = iSourceLen / 1024;
iLeaveBytes = iSourceLen - iReadTimes * 1024; if(!(fpTarget = fopen("019bak.bmp","wb")))
{
AfxMessageBox("Create Target File Error!");
return;
}
dwSourceLen = 1024 ;
dwTargetLen = 2048 ;
fseek(fpSource,0,SEEK_SET);
for(int i = 0 ; i < iReadTimes ; i++)
{
iCompRet = -1 ;
memset(bSource,0,1024);
memset(bTarget,0,2048);
fread(bSource,1024,1,fpSource);
iCompRet = compress(bTarget,&dwTargetLen,bSource,dwSourceLen);
fwrite(bTarget,dwTargetLen,1,fpTarget);
} memset(bSource,0,1024);
memset(bTarget,0,2048);
dwSourceLen = dwTargetLen = iLeaveBytes ;
fread(bSource,iLeaveBytes,1,fpSource);
iCompRet = compress(bTarget,&dwTargetLen,bSource,dwSourceLen);
fwrite(bTarget,dwTargetLen,1,fpTarget); if(fpSource) fclose(fpSource);
if(fpTarget) fclose(fpTarget);
//End压出来的结果与长度严重不对!
------------------------
::高级编程小书童::
for(int i = 0 ; i < iReadTimes ; i++)
{
iCompRet = -1 ;
memset(bSource,0,1024);
memset(bTarget,0,2048);
fread(bSource,1024,1,fpSource);
iCompRet = compress(bTarget,&dwTargetLen,bSource,dwSourceLen);
fwrite(bTarget,dwTargetLen,1,fpTarget);
}
我果然说得不错,问题就在上面,注意到compress中dwTargetLen是一个in\out参数,就是说它的输入也是有意义的,输出也是有意义的,你第一次运行的时候dwTargetlen 为2048,压缩第一次以后这个值假设为1000,但是第二次压缩compress就会认为你的目标缓冲就是1000个字节,如果它第二次压缩以后要求的长度超过1000就会出现你给出的那个错误。
你应该在每次压缩前重新设置dwTargetLen的值为2048你的程序写得太不好了,我竟然看到
memset(xxx, 0, 1024);
这是很不好的风格,好的风格应该是这样
memset(xxx, 0, sizeof(xxx));
这样几十xxx的定义修改为char xxx[100];也不会有问题,要照你那样写就肯定有问题。另外你读文件的时候也有问题,
fread(bSource,1024,1,fpSource);
你这样读如果读出的数据够1024,那么返回为1,而不是1024,你应该这样写
fread(bSource, 1, 1024, fpSource);
#include "stdafx.h"
#include "zlib.h"
#include <string.h>
int main(int argc, char* argv[])
{
const Bytef d[]="恶心恶心恶心恶心恶心恶心恶心恶心";
printf("%s\n", d);
Bytef* o=new Bytef[75];
uLong e=75L;
int result = compress(o, &e, d, strlen((const char*)d));
if( Z_OK == result )
{
printf("压缩尺寸:%d\n", e);
}
else
{
printf("错误!\n");
}
delete[] o;
return 0;
}
error LNK2001: unresolved external symbol _compress
[email protected]