Ruby zip file in memory
Last update
2017-07-31
2017-07-31
«zip & gzip compression»
GZIP/GUNZIP a string:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class String # http://ruby-doc.org/stdlib-2.2.0/libdoc/zlib/rdoc/Zlib/GzipWriter.html # https://gist.github.com/sinisterchipmunk/1335041 def gzipped(fname = 'file.txt') gz_file = StringIO.new z = Zlib::GzipWriter.new(gz_file) z.mtime = Time.now # for gzip compat z.orig_name = fname # for gzip compat z.write self z.close gz_file.string end # gzipped def gunzipped gz_file = StringIO.new self z = Zlib::GzipReader.new(gz_file) z.read end # gunzipped end |
Create a ZIP archive on the fly:
1 2 3 4 5 6 7 | require 'zip' # https://github.com/rubyzip/rubyzip # Zip.default_compression = Zlib::BEST_COMPRESSION Zip::File.open("archive.zip", Zip::File::CREATE) do |zipfile| zipfile.get_output_stream("hello.txt"){|f| f.write "hello world" } end |