使用 zlib 时解压缩错误 [英] uncompress error when using zlib

查看:52
本文介绍了使用 zlib 时解压缩错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题.当我尝试解压缩内存中的压缩数据时,出现错误.这是代码.

It could be a silly question. When I tried to uncompress a compressed data in memory, got error. Here is the code.

#include <zlib.h>
#include <stdio.h>
#include <stdlib.h>

int readFile(char *fname, char buf[]) {
    FILE *fp = fopen(fname, "rb");
    if (fp == NULL) { printf("Failed to open %s\n", fname); exit(0);}
    int n = fread(buf, 1, 0x100000, fp);
    fclose(fp);
    return n;
}

char buf[2][0x10000];
int main(int argc, char *argv[]) {
    long n = readFile(argv[1], &buf[0][0]);
    unsigned int *pInt = (unsigned int*) (&buf[0][0]);
    printf("n=%d %08x\n", n, *pInt);
    long m = 0x10000;
    int rc = uncompress(&buf[1][0], &m, &buf[0][0], n);
    printf("rc = %d %s\n", rc, &buf[1][0]);
    return 0;
}

出现错误:

./a.out te.html.gz
n=169 08088b1f
rc = -3 

te.html.gz 是通过运行`gzip te.html'获得的.

te.html.gz is obtained from by running `gzip te.html'.

谢谢!

推荐答案

zlib 格式不是 gzip 格式.zlib uncompress 函数无法识别 gzip 格式.

zlib format is not gzip format. The zlib uncompress function doesn't understand the gzip format.

您可以通过编写一个调用zlib中的compress函数的类似程序来生成一些zlib格式的测试数据.或者你可以使用 openssl zlib 命令,如果你已经安装了 openssl.

You can generate some zlib format test data by writing a similar program that calls the compress function in zlib. Or you could use the openssl zlib command, if you have openssl installed.

这篇关于使用 zlib 时解压缩错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆