Skip to content

Instantly share code, notes, and snippets.

@tbalthazar
Created January 14, 2009 09:15
Show Gist options
  • Save tbalthazar/46848 to your computer and use it in GitHub Desktop.
Save tbalthazar/46848 to your computer and use it in GitHub Desktop.
# http://discuss.joyent.com/viewtopic.php?id=9043
require 'zip/zipfilesystem'
# source should be a zip file.
# target should be a directory to output the contents to.
def unzip_file(source, target)
# Create the target directory.
# We'll ignore the error scenario where
begin
Dir.mkdir(target) unless File.exists? target
end
Zip::ZipFile.open(source) do |zipfile|
dir = zipfile.dir
dir.entries('.').each do |entry|
zipfile.extract(entry, "#{target}/#{entry}")
end
end
rescue Zip::ZipDestinationFileExistsError => ex
# I'm going to ignore this and just overwrite the files.
rescue => ex
puts ex
end
unzip_file('/home/styrmis/workspace/ruby/unzip/test.zip', '/home/styrmis/workspace/ruby/unzip/target')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment