1
votes

I'm trying to generate gzip compressed file from xml using the 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;
    }

   for ( ; ; ) 
   { 
      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 I test on is 2 KB, and the result seems always corrupted. Note: when I remove some data from the file, it seems working (Then I tested it on different input file and got the same result).

1

1 Answers

0
votes

First off, int readed and int written are not used, and n and w are not set. Perhaps you meant int n and int w?

Second, the else if (status != Z_OK) is too stringent. You may also get a Z_BUF_ERROR, for which you should continue, not error out. Z_BUF_ERROR is not really an error, it's just a warning that no progress was made on that last call. You can simply continue from there, providing more input and/or more output space.

Other points: you probably don't want to use Z_SYNC_FLUSH for every buffer. That degrades compression needlessly, unless you have a reason to insert sync markers. Just use Z_NO_FLUSH. Also a memLevel of 9 may reduce compression. You should experiment with your data. Generally 8 (the default) works better.