How can I copy a directory inside a zip archive to a second zip archive using rubyzip?
Question
I have a .zip archive containing several directories. Using the rubyzip gem I would like to reach into the .zip archive, copy a specified directory (and its contents) and move the directory into a second .zip archive.
Ideally I would not have to extract the contents of the first .zip archive, then re-zip them into a second archive. I'm hoping there is a way to use methods provided in the rubyzip gem.
Solution
After checking with one of the maintainers of the rubyzip gem, I have learned that this is not possible.
OTHER TIPS
The RubyZip library must have been updated since then to support it. This worked for me.
require 'rubygems'
require 'zip' # gem 'rubyzip', '>= 1.0'
Zip::File.open('large.zip', false) do |input|
Zip::File.open('small.zip', true) do |output|
input.glob('my_folder_name/*') do |entry|
entry.get_input_stream do |input_entry_stream|
output.get_output_stream(entry.name) do |output_entry_stream|
# you could also chunk this, rather than reading it all at once.
output_entry_stream.write(input_entry_stream.read)
end
end
end
end
end
For versions of RubyZip < 1.0, require 'zip/zip'
instead, and use Zip::ZipFile
instead of Zip::File
This is a bit of a brute force method (and may or may not work for your application), but you could copy the entire first zip file and then use rubyzip methods to delete everything but the target directory from the copied file.
Theoretically what you are asking should be possible if you are using deflate compression (which stores every file as an individually compressed item).