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).