This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'zlib' | |
require 'stringio' | |
require 'json' | |
def gunzip(data) | |
io = StringIO.new(data, "rb") | |
gz = Zlib::GzipReader.new(io) | |
decompressed = gz.read | |
end | |
def gzip(string) | |
wio = StringIO.new("w") | |
w_gz = Zlib::GzipWriter.new(wio) | |
w_gz.write(string) | |
w_gz.close | |
compressed = wio.string | |
end | |
data = (1..100).collect {|i| {"id_#{i}" => "value_#{i}" } } | |
contents = data.to_json | |
compressed = gzip(contents) | |
decompressed = gunzip(compressed) | |
puts "compressed string:" | |
puts compressed | |
puts "decompressed success? #{decompressed == contents}" | |
puts "uncompressed data size: #{contents.length}" | |
puts "decompressed data size: #{compressed.length}" | |
puts "compress ratio: #{compressed.length.to_f / contents.length.to_f}" |