c++ - ZLib gZip compress generates corrupted result -
i'm trying generate gzip compressed file xml using following code:
stream.next_in = inbuf; stream.avail_in = 0; stream.next_out = outbuf; stream.avail_out = mybuffersize; int infile_remaining = infilesize; if (deflateinit2(&stream, z_default_compression, z_deflated, windowsbits + 16, 9, z_default_strategy) != z_ok) { return 0; } ( ; ; ) { int status; if (!stream.avail_in) { int n = min(buffer_size, infile_remaining); if (myfilereadfunc(pinfile, inbuf, n) != n) { deflateend(&stream); return -1; } stream.next_in = inbuf; stream.avail_in = n; infile_remaining -= n; } status = deflate(&stream, infile_remaining ? z_no_flush : z_finish); if ((status == z_stream_end) || (!stream.avail_out)) { int w = mybuffersize - stream.avail_out; if (myfilewritefunc(poutfile, outbuf, w) != w) { deflateend(&stream); return -1; } stream.next_out = outbuf; stream.avail_out = mybuffersize; } if (status == z_stream_end) break; else if (status != z_ok) { deflateend(&stream); return 0; } } if (deflateend(&stream) != z_ok) { return 0; }
the input xml file test on 2 kb, , result seems corrupted. note: when remove data file, seems working (then tested on different input file , got same result).
first off, int readed
, int written
not used, , n
, w
not set. perhaps meant int n
, int w
?
second, else if (status != z_ok)
stringent. may z_buf_error
, should continue, not error out. z_buf_error
not error, it's warning no progress made on last call. can continue there, providing more input and/or more output space.
other points: don't want use z_sync_flush
every buffer. degrades compression needlessly, unless have reason insert sync markers. use z_no_flush
. memlevel
of 9 may reduce compression. should experiment data. 8 (the default) works better.
Comments
Post a Comment