求大神指导关于gzip解压,出问题!

根根小盆友 2013-03-26 10:59:35
我从http上下载了gizp格式的数据,用解压缩软件已经解压成功,我现在想自己写解压代码,发现解压之后的都是空白,不知道什么原因,解压代码如下
#include <stdio.h>    
#include "zlib.h"
#pragma comment(lib, "./zlib1.lib")

int main()
{
/* 原始数据 */
FILE* p=fopen("mysocket_test.gzip","rb+");
FILE* p2=fopen("123.txt","wb");

unsigned char buf[104857] = {0};
unsigned char strDst[104857] = {0};
unsigned long bufLen = sizeof(buf);
unsigned long dstLen = sizeof(strDst);
fread(buf,1,145910,p);

/* 解压缩 */
uncompress(strDst, &dstLen, buf, 145910);
printf("%s\n",strDst);
fwrite(strDst,1,dstLen,p2);
return 0;
}
其中mysocket_test.gzip放着的是gzip压缩的数据,大小是145910.
...全文
211 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-03-27
  • 打赏
  • 举报
回复
看调sample代码是程序员必须掌握的技能之一。 http://bbs.csdn.net/topics/390374955
根根小盆友 2013-03-26
  • 打赏
  • 举报
回复
引用 2 楼 qn20080206 的回复:
引用 1 楼 chuachua66 的回复: 大哥你压缩了以后都145910了,你解压的buffer才104857,肯定放不下 啊,另外,看Unconpress的返回值,zlib.h里有错误吗的描述。大哥,分配的是不够,我改成了1048576,结果还是空白,返回值是Z_DATA_ERROR,怎么解决啊?实在是不了解zlib
我把buf改成1048576,然后fread返回值是145910,然后返回值还是Z_DATA_ERROR
chuachua66 2013-03-26
  • 打赏
  • 举报
回复
引用 2 楼 qn20080206 的回复:
引用 1 楼 chuachua66 的回复:大哥你压缩了以后都145910了,你解压的buffer才104857,肯定放不下 啊,另外,看Unconpress的返回值,zlib.h里有错误吗的描述。大哥,分配的是不够,我改成了1048576,结果还是空白,返回值是Z_DATA_ERROR,怎么解决啊?实在是不了解zlib
Z_DATA_ERROR if the input data was corrupted or incomplete,你输入的数据有问题啊,把你读入数据的buf搞大,然后看fread返回值,看看是不是有东西没读进来。
根根小盆友 2013-03-26
  • 打赏
  • 举报
回复
引用 1 楼 chuachua66 的回复:
大哥你压缩了以后都145910了,你解压的buffer才104857,肯定放不下 啊,另外,看Unconpress的返回值,zlib.h里有错误吗的描述。
大哥,分配的是不够,我改成了1048576,结果还是空白,返回值是Z_DATA_ERROR,怎么解决啊?实在是不了解zlib
chuachua66 2013-03-26
  • 打赏
  • 举报
回复
大哥你压缩了以后都145910了,你解压的buffer才104857,肯定放不下 啊,另外,看Unconpress的返回值,zlib.h里有错误吗的描述。
根根小盆友 2013-03-26
  • 打赏
  • 举报
回复
谢谢大家了,我解决这个问题了,因为http上的gzip多了个gzip头,不能直接用zlib进行解压,需要先处理一下。十分感谢大家
gqjjqg 2013-03-26
  • 打赏
  • 举报
回复
引用 4 楼 qn20080206 的回复:
引用 2 楼 qn20080206 的回复:引用 1 楼 chuachua66 的回复: 大哥你压缩了以后都145910了,你解压的buffer才104857,肯定放不下 啊,另外,看Unconpress的返回值,zlib.h里有错误吗的描述。大哥,分配的是不够,我改成了1048576,结果还是空白,返回值是Z_DATA_ERROR,怎么解决啊?实在是不了解zlib我把……
zlib 里有sample code。请参阅。我看的是 V1.2.7的版本。

/* ===========================================================================
 * Test compress() and uncompress()
 */
void test_compress(compr, comprLen, uncompr, uncomprLen)
    Byte *compr, *uncompr;
    uLong comprLen, uncomprLen;
{
    int err;
    uLong len = (uLong)strlen(hello)+1;

    err = compress(compr, &comprLen, (const Bytef*)hello, len);
    CHECK_ERR(err, "compress");

    strcpy((char*)uncompr, "garbage");

    err = uncompress(uncompr, &uncomprLen, compr, comprLen);
    CHECK_ERR(err, "uncompress");

    if (strcmp((char*)uncompr, hello)) {
        fprintf(stderr, "bad uncompress\n");
        exit(1);
    } else {
        printf("uncompress(): %s\n", (char *)uncompr);
    }
}

/* ===========================================================================
 * Usage:  example [output.gz  [input.gz]]
 */

int main(argc, argv)
    int argc;
    char *argv[];
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    static const char* myVersion = ZLIB_VERSION;

    if (zlibVersion()[0] != myVersion[0]) {
        fprintf(stderr, "incompatible zlib version\n");
        exit(1);

    } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
        fprintf(stderr, "warning: different zlib version\n");
    }

    printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
            ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());

    compr    = (Byte*)calloc((uInt)comprLen, 1);
    uncompr  = (Byte*)calloc((uInt)uncomprLen, 1);
    /* compr and uncompr are cleared to avoid reading uninitialized
     * data and to ensure that uncompr compresses well.
     */
    if (compr == Z_NULL || uncompr == Z_NULL) {
        printf("out of memory\n");
        exit(1);
    }

#ifdef Z_SOLO
    argc = strlen(argv[0]);
#else
    test_compress(compr, comprLen, uncompr, uncomprLen);

    test_gzio((argc > 1 ? argv[1] : TESTFILE),
              uncompr, uncomprLen);
#endif

    test_deflate(compr, comprLen);
    test_inflate(compr, comprLen, uncompr, uncomprLen);

    test_large_deflate(compr, comprLen, uncompr, uncomprLen);
    test_large_inflate(compr, comprLen, uncompr, uncomprLen);

    test_flush(compr, &comprLen);
    test_sync(compr, comprLen, uncompr, uncomprLen);
    comprLen = uncomprLen;

    test_dict_deflate(compr, comprLen);
    test_dict_inflate(compr, comprLen, uncompr, uncomprLen);

    free(compr);
    free(uncompr);

    return 0;
}

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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