Zip files with Ruby

Updated . Posted . Visible to the public.

When you need to zip up files in Ruby, use zipruby.

sudo gem install zipruby

You can add existing files, add files from strings and even add directories.
Example usage:

require 'zipruby'
cars = %w[audi bmw mercedes]

zipfile = Tempfile.new('my.zip', 'tmp')
Zip::Archive.open(zipfile.path, Zip::CREATE) do |zip|
  zip.add_file '/tmp/me.txt'
  zip.add_dir 'cars' 

  cars.each do |car|
    zip.add_buffer "cars/#{car}.txt", "This #{car} is mine!" 
  end
end

Credits go to winebarrel for the Ruby bindings and to Baron & Klausner for libzip. More examples at the attached link.

Dominik Schöler
Last edit
Daniel Straßner
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2011-07-26 18:29)