fredrik.j
fredrik.j

Reputation: 63

"Convert" zlib stream to gzip?

I have a blob of data which is compressed with zlib, now I need to decompress it but only have gzip at hand in a Linux environment. No possibility of getting any other tools unfortunately. Is there any way to "convert" the zlib data into something that gzip can decompress?

I read this one: https://unix.stackexchange.com/questions/22834/how-to-uncompress-zlib-data-in-unix

Where it's basically said to add a 10 bytes header and pass it into gzip. However, when I try this I don't get any output. The out file is empty.

printf "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00" |cat - /tmp/data |gzip -dc >/tmp/out

/tmp/data contains a chunk of data that starts with these two bytes: \x78\xDA (which I interpret as a zlib header).

Any suggestions? I've also tried removing these 2 bytes before prepending the gzip header.

Edit: After some more thinking and after reading the comments here I realized that the input data was in fact not correct. Using another source for test data I got a bit further. See below, encoded as base64 since I could not attach binary data.

I have the source data:

eJwNzbEvA1EcwPEcgsUs6UIisZ17v7tre2ehaR+Kq/TaU0XEvddTd9yrtq8NFiQ1GGwisWnSQQSJwWoSJgMWxGBCYvEPEB2/y+f7WIvWhf5629jWXE/ShJ/erNDVEEgMtwXIydZ9e2DxbtLQhda60LCrO6eEuuuSZSYGrelofBBBDEvxWZAVNRiRZMBofCYWtpLZEI5OGkomHc6O9GmEFpoeybEWkuOejGQQkSoiIG6ZmwXilHjvRIEui3FW5i6vcOLb/pQ5KgJCSAHQVOKvrWJLAklGEgRRmJRzLYTnXU1FQUVHKESqax4A6IrarDDNFYgHeqj50ESQKbP9BllinlEpc6fk24yRPHNwyV2x6RLjnmGl0tg0IokEzTPuYDM+EaFVp+TAABpAM8Pjtcc9u/PrYdHJFK+f37vfAocdmxf5s5uUmJ73Lz86F/z93233c+P1++mlOPR369GrVqzvnh8dH95qB/QfS/18rQ==

Then I strip the two first bytes, which gives me this:

Dc2xLwNRHMDxHILFLOlCIrGde7+7a3tnoWkfiqv02lNFxL3XU3fcq7avDRYkNRhsIrFp0kEEicFqEiYDFsRgQmLxDxAdv8vn+1iL1oX+etvY1lxP0oSf3qzQ1RBIDLcFyMnWfXtg8W7S0IXWutCwqzunhLrrkmUmBq3paHwQQQxL8VmQFTUYkWTAaHwmFraS2RCOThpKJh3OjvRphBaaHsmxFpLjnoxkEJEqIiBumZsF4pR470SBLotxVuYur3Di2/6UOSoCQkgB0FTir61iSwJJRhIEUZiUcy2E511NRUFFRyhEqmseAOiK2qwwzRWIB3qo+dBEkCmz/QZZYp5RKXOn5NuMkTxzcMldsekS455hpdLYNCKJBM0z7mAzPhGhVafkwAAaQDPD47XHPbvz62HRyRSvn9+73wKHHZsX+bOblJie9y8/Ohf8/d9t93Pj9fvppTj0d+vRq1as754fHR/eagf0H0v9fK0=

Writing that (in binary, not base64 - just to be clear) to a file: /tmp/inputdata I run the following shell command:

printf "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00" |cat - /tmp/inputdata| gzip -dc >/tmp/out

Which gives me this:

gzip: corrupted data

The contents of /tmp/out (again, base64 encoded):

0oRDoQEmoQRIf1sfUVIx8CBZAQ2kAWJERQQaYqh/zQYaYMdMTTkBA6EBpGF2gapiY2l4L1VSTjpVVkNJOjAxREUvSVoxMjM0NUEvMjFFMEpYRDdVUVk2RUNMTTNXVDdZRiM4YmNvYkRFYmRuAmJkdGoyMDIxLTA0LTAxYmlzdFJvYmVydCBLb2NoLUluc3RpdHV0Ym1hbU9SRy0xMDAwMzExODRibXBsRVUvMS8yMC8xNTA3YnNkAmJ0Z2k4NDA1MzkwMDZidnBqMTExOTM0OTAwN2Nkb2JqMTk2NC0wOC0xMmNuYW2kYmZuak11c3Rlcm1hbm5iZ25lRXJpa2FjZm50ak1VU1RFUk1BTk5jZ250ZUVSSUtBY3ZlcmUxLjAuMFhASoTSiWEI6NFgZVdxvtjgF9walgd6rmesxFMtVFxtseYIXm2N/YBp53na69PZcT/+xmpjtQNFOYWtmaCWxjiUYw==

Upvotes: 0

Views: 665

Answers (1)

Mark Adler
Mark Adler

Reputation: 112547

You got it right. Strip the first two bytes and prepend those ten bytes. Then gzip -dc will complain about an "unexpected end of file", but it will nevertheless decompress and write the result to your output file.

Unless the zlib stream is corrupt to begin with.

Upvotes: 1

Related Questions