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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -