Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby zlib deflate massive data

I'm trying to use Zlib::Deflate.deflate on a massive file (4 gigs). There are obvious problems with doing that, the first of which being that I can't load the entire file into memory all at once. Zlib::GzipWriter would work, since it works with streams, but it's not zlib compression. Any ideas?

like image 344
Bub Bradlee Avatar asked Mar 11 '26 02:03

Bub Bradlee


1 Answers

You could try instantiating a Zlib::Deflate stream and feeding it data from your big file piecemeal. Zlib::Deflate::deflate purports to do that sort of thing behind the scenes.

It would look something like this:

z = Zlib::Deflate.new

File.open "big_uncompressed_file" do |f|
  File.open "big_compressed_file", "w" do |w|
    f.each do |str|
      w << z.deflate str, Zlib::SYNC_FLUSH
    end
  end
end
z.finish
z.close

ruby zlib docs

notes on zlib flush flags

like image 57
BaroqueBobcat Avatar answered Mar 12 '26 18:03

BaroqueBobcat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!