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

5

大家好,

我正在尝试使用dev-c++让zpipe演示程序运行,我已经导入了所有的zlib代码,并使用zpipe.c作为示例。一切都编译并运行正常。如果我尝试使用注释掉的deflateInit2调用创建gzip文件,它会在没有错误的情况下创建,但在使用7zip解压缩时会损坏。如果我使用标准的zlib头文件来创建文件,在使用相应的inflate调用进行解压缩时,它会给我返回-3/Z_DATA_ERROR,表示我的defalte数据已损坏。一切都指向def函数出现问题,但它几乎完全按照示例。

有什么想法吗?非常感谢!

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

1
在Windows上,你难道不应该使用“rb”和“wb”打开二进制文件吗? - Omri Barel
如果你正在重定向stdin和stdout,请注意:不要使用PowerShell的Get-Content <input file> | ./zpipe -d > <output file>,它会报告“zpipe:无效或不完整的deflate数据”(CMD的“>”和“<”则可以正常工作)。 - broderick
1个回答

3

但这就是一个很好的例子。

可惜的是,您忽略了来自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)

或者您需要在两个fopen()函数中使用"b"选项。


我有包含和定义,但是“丢失”了对SET_BINARY_MODE的调用。非常感谢! - schmouder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接