请问为何我用Zlib库来压缩一段数据(用compress()函数),为何第一个次的返回值为正常的:0(Z_OK),再用返回值就是-5(Z_BUF_ERROR)了.谢谢!

解决方案 »

  1.   

    //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压出来的结果与长度严重不对!
      

  2.   

    帮你up...顺便问一下,我有个zlib例子里面,压缩前和解压缩后的数据为什么会不一样?是对操作系统有要求吗?
    ------------------------
    ::高级编程小书童::
      

  3.   

    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);
      

  4.   

    http://www.csdn.net/expert/topic/1043/1043012.xml?temp=2.820987E-02
      

  5.   

    to harry202(harry) 当然不一样了……
      

  6.   

    多谢Oldworm的批评.我会好好努力的.
      

  7.   

    Oldworm 如果没有问题,他来这里问什么?如果你能决绝你写代码。何必在这里说那么许多废话!
      

  8.   

    /*本来要下去睡觉……我最讨厌评论员文章!*/
    #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;
    }
      

  9.   

    SWF to CWS 他的压缩方式是分块 压缩 头压缩 其他部分也压缩 本身SWF也采用了Zlib压缩了!如果你用全不文件压缩 那当然只有头符合要求了!
      

  10.   

    其他部分 你只是 做了 重复压缩的工作明白了吗????!!!!看来你的问题不在ZLIB上 而是在CWS压缩 格式 和分块上!你现在研究的问题 已经可以脱离 Zlib了!
      

  11.   

    头已经符合了…… 那说明你Zlib已经压缩正常了…… 没有任何问题了!
      

  12.   

    哪位有Zlib的例子,可以发给我一份吗,我的总是编译出错
     error LNK2001: unresolved external symbol _compress
    [email protected]