How to decompress Gzip string in ruby?

RubyGzipZlib

Ruby Problem Overview


Zlib::GzipReader can take "an IO, or IO-like, object." as it's input, as stated in docs.

Zlib::GzipReader.open('hoge.gz') {|gz|
  print gz.read
}

File.open('hoge.gz') do |f|
  gz = Zlib::GzipReader.new(f)
  print gz.read
  gz.close
end

How should I ungzip a string?

Ruby Solutions


Solution 1 - Ruby

The above method didn't work for me.
I kept getting incorrect header check (Zlib::DataError) error. Apparently it assumes you have a header by default, which may not always be the case.

The work around that I implemented was:

require 'zlib'
require 'stringio'
gz = Zlib::GzipReader.new(StringIO.new(resp.body.to_s))    
uncompressed_string = gz.read

Solution 2 - Ruby

Zlib by default asumes that your compressed data contains a header. If your data does NOT contain a header it will fail by raising a Zlib::DataError.

You can tell Zlib to assume the data has no header via the following workaround:

def inflate(string)
  zstream = Zlib::Inflate.new(-Zlib::MAX_WBITS)
  buf = zstream.inflate(string)
  zstream.finish
  zstream.close
  buf
end

Solution 3 - Ruby

You need Zlib::Inflate for decompression of a string and Zlib::Deflate for compression

  def inflate(string)
    zstream = Zlib::Inflate.new
    buf = zstream.inflate(string)
    zstream.finish
    zstream.close
    buf
  end

Solution 4 - Ruby

In Rails you can use:

  • ActiveSupport::Gzip.compress("my string")
  • ActiveSupport::Gzip.decompress().

Solution 5 - Ruby

zstream = Zlib::Inflate.new(16+Zlib::MAX_WBITS)

Solution 6 - Ruby

Using (-Zlib::MAX_WBITS), I got ERROR: invalid code lengths set and ERROR: invalid block type
The only following works for me, too.

Zlib::GzipReader.new(StringIO.new(response_body)).read

Solution 7 - Ruby

I used the answer above to use a Zlib::Deflate

I kept getting broken files (for small files) and it took many hours to figure out that the problem can be fixed using:

buf = zstream.deflate(string,Zlib::FINISH)

without the the zstream.finish line!

def self.deflate(string)
	zstream = Zlib::Deflate.new
	buf = zstream.deflate(string,Zlib::FINISH)
	zstream.close
	buf
end

Solution 8 - Ruby

To gunzip content, use following code (tested on 1.9.2)

Zlib::GzipReader.new(StringIO.new(content), :external_encoding => content.encoding).read

Beware of encoding problems

Solution 9 - Ruby

We don't need any extra parameters these days. There are deflate and inflate class methods which allow for quick oneliners like these:

>> data = "Hello, Zlib!"
>> compressed = Zlib::Deflate.deflate(data)
=> "x\234\363H\315\311\311\327Q\210\312\311LR\004\000\032\305\003\363"
>> uncompressed = Zlib::Inflate.inflate(compressed)
=> "Hello, Zlib!"

I think it answers the question "How should I ungzip a string?" the best. :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFluffyView Question on Stackoverflow
Solution 1 - RubyGarthView Answer on Stackoverflow
Solution 2 - Rubyuser211577View Answer on Stackoverflow
Solution 3 - RubydimusView Answer on Stackoverflow
Solution 4 - RubyTylerView Answer on Stackoverflow
Solution 5 - RubyBearPyView Answer on Stackoverflow
Solution 6 - RubyjohnView Answer on Stackoverflow
Solution 7 - RubyAbdoView Answer on Stackoverflow
Solution 8 - Rubypinguin666View Answer on Stackoverflow
Solution 9 - RubyAlex FortunaView Answer on Stackoverflow