0

I am able to compress certain content with "gzip" format and store it in a file. This content can be decompressed programatically within my code.
But I can't "gunzip" them using command line or other tools. e.g. "Hello World" (11 bytes) is compressed properly as 'test.txt.gz', but upon double clicking or from command line, it gives below error:

enter image description here

On the other hand, if I store the same content in the .txt file and gzip that .txt, then it's decompressed properly.

What is the correct way to store gzip content into a file?


Here is the C++14 source code used for compression/decompression (Referred from: Compress string with GZip using qCompress?).
Hex dump (xxd -p /home/milind/Desktopp/test.txt.gz):

1f8b080000000000000b789cf348cdc9c95728cf2fca49010018ab043d52
9ed68b0b000000

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • You need to show your code for how you are compressing and storing. Also provide a hex dump of the resulting file that cannot be extracted. Edit your question with this information. Do not provide it in comments. – Mark Adler Aug 21 '21 at 14:34
  • @MarkAdler, I have added code & hexdump in the post. Please let me know if it's correct or anything else is required. Thanks. – iammilind Aug 23 '21 at 04:52

1 Answers1

2

The problem is that you wrote a gzip header, and then followed that with a zlib-format stream! You need to follow the gzip header with raw deflate data, with no zlib-format wrapper.

Don't use compress2(), which can only make a zlib stream. Use the deflate functions, documented in zlib.h. You can request that deflate produce a gzip stream, so you don't need to write the header yourself, and you don't need to do your own CRC-32 calculation. That is all provided by zlib.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • For "gzip" creation we have to set `windowBits` as `15 | 16`. What should be the value for "zip" creation? As per [this post](https://stackoverflow.com/questions/33506713/how-to-make-a-zip-file-using-zlib) is it not directly supported? – iammilind Aug 25 '21 at 12:00
  • zlib does not support the zip format directly. You would use raw deflate and raw inflate, and write your own code to processs zip headers and trailers around that and the central directory. – Mark Adler Aug 25 '21 at 16:17