gpt4 book ai didi

c - 使用 zlib 使用 zpipe.c 示例创建 gzip 文件

转载 作者:行者123 更新时间:2023-12-04 12:05:33 25 4
gpt4 key购买 nike

全部,

我只是想让 zpipe 演示使用 dev-c++ 工作,其中我导入了所有 zlib 代码并使用 zpipe.c 作为示例。一切都编译并运行。如果我尝试使用注释掉的对 deflateInit2 的调用创建一个 gzip 文件,它会创建现在错误,但在使用 7zip 解压缩时已损坏。如果我使用标准的 zlib 头文件来创建文件,当我使用相应的调用来膨胀时,它会返回 -3/Z_DATA_ERROR 指示我的默认数据已损坏。一切都指向定义中的错误,但它几乎正是示例。

有任何想法吗??非常感谢!

int main(int argc, char **argv)
{
int ret;
FILE *source;
FILE *zip;
FILE *zipped;
FILE *back;

source = fopen ("C:\\Users\\schmoudm\\Pictures\\caela.jpg", "r");
zip = fopen ("C:\\Core\\RD\\test.gz", "w");

printf ("calling def \n");
ret = def(source, zip, Z_DEFAULT_COMPRESSION);
printf("def return: %i \n", ret);
fclose(source);
fclose(zip);

if (ret == 0) {
printf ("setting up inf \n");
zipped = fopen ("C:\\Core\\RD\\test.gz", "r");
back = fopen ("C:\\Core\\RD\\zlibout.txt", "w");
printf ("calling inf \n");
ret = inf(zipped, back);
printf("inf return: %i \n", ret);
zerr(ret);
}

fclose(source);
fclose(zip);

printf("DONE!");
system("PAUSE");
return 0;
}

int def(FILE *source, FILE *dest, int level)
{
int ret, flush;
unsigned have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];

/* allocate deflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;

ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
//ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY);
if (ret != Z_OK)
return ret;

/* compress until end of file */
do {
strm.avail_in = fread(in, 1, CHUNK, source);
printf("available in: %u \n", strm.avail_in);
if (ferror(source)) {
(void)deflateEnd(&strm);
return Z_ERRNO;
}
flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
strm.next_in = in;

/* run deflate() on input until output buffer not full, finish
compression if all of source has been read in */
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = deflate(&strm, flush); /* no bad return value */
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
have = CHUNK - strm.avail_out;
if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
(void)deflateEnd(&strm);
return Z_ERRNO;
}
} while (strm.avail_out == 0);
assert(strm.avail_in == 0); /* all input will be used */

/* done when last data in file processed */
} while (flush != Z_FINISH);
assert(ret == Z_STREAM_END); /* stream will be complete */

/* clean up and return */
(void)deflateEnd(&strm);
return Z_OK;
}

最佳答案

but its pretty much exactly the example.



唉,您遗漏了 zpipe.c 中的关键部分.
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
#else
# define SET_BINARY_MODE(file)
#endif


/* avoid end-of-line conversions */
SET_BINARY_MODE(stdin);
SET_BINARY_MODE(stdout);

所以在你的代码中你需要做 SET_BINARY_MODE(source)SET_BINARY_MODE(zip) .

或者您需要使用 "b"两个选项 fopen()的。

关于c - 使用 zlib 使用 zpipe.c 示例创建 gzip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12115829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com