Marcus Ilgner
Marcus Ilgner

Reputation: 7211

Ruby: uncompress zlib-wrapped deflate data

In Ruby, I have a buffer containing data compressed with the zlib compress2() method. However I found no way to decompress this data using the Zlib functionality in the Ruby standard library which only supports data created by deflate or data in GZip format.

How can I achieve the equivalent of uncompress() in Ruby, preferably without resorting to creating a custom C-extension?

Edit:

I found the solution. After fiddling around with the window_bits argument to the Inflate constructor without success, I finally understood that zlib prefixes the compressed data with a four-byte header. So I simply removed that header and suddenly it worked like a charm:

  data[0..3] = ''
  data = Zlib::Inflate.inflate(data)

Upvotes: 4

Views: 769

Answers (2)

Andrii Lazarchuk
Andrii Lazarchuk

Reputation: 21

You need to use negative value for window_bits as stated here. I've faced similar problem but for compressing on Ruby and decompressing on JS (my gist for that). Hope it helps to avoid magic with four bites :)

Upvotes: 2

Mark Adler
Mark Adler

Reputation: 112374

The documentation indicates that the Ruby inflate class will decompress the output of compress2(), which is in the zlib format. I just tried it, and it works fine. Your compressed data may not be making it over to Ruby intact.

Upvotes: 1

Related Questions