Skip to content

Instantly share code, notes, and snippets.

@pioz
Created October 14, 2011 21:46
Show Gist options
  • Select an option

  • Save pioz/1288451 to your computer and use it in GitHub Desktop.

Select an option

Save pioz/1288451 to your computer and use it in GitHub Desktop.
Zip files and directories recursively
require 'zip/zip'
module Zip
def self.create(filename, *files)
Zip::ZipFile.open(filename, Zip::ZipFile::CREATE) do |zip|
files.each { |file| add(zip, file) }
end
end
private
def self.add(zip, file)
if File.directory?(file)
begin
zip.mkdir(file)
rescue Errno::EEXIST; end
Dir["#{file}/**"].each do |ffile|
add(zip, ffile)
end
else
begin
zip.add(file, file)
rescue Zip::ZipEntryExistsError; end
end
end
end
Zip.create('test.zip', *ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment