Created
October 14, 2011 21:46
-
-
Save pioz/1288451 to your computer and use it in GitHub Desktop.
Zip files and directories recursively
This file contains hidden or 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 '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