Last active
October 8, 2017 11:53
-
-
Save ampedandwired/3385002 to your computer and use it in GitHub Desktop.
Zip a directory to memory in Ruby. The rubyzip library is pretty hard to use. I tried for ages to figure out how to zip a directory to a string in memory. So here's an example that zips the given directory to a Ruby StringIO object using rubyzip.
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' | |
def zip(dir) | |
Zip::ZipOutputStream::write_buffer do |zos| | |
Dir["#{dir}/**/**"].each do |file| | |
path_for_file_in_zip = file.sub(/\A#{dir}\//, '') | |
if !File.directory?(file) | |
zip_entry = zos.put_next_entry(path_for_file_in_zip) | |
zos << IO.read(file) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment