How can I copy a directory in a zip archive to a second zip archive using rubyzip?
I have a ZIP archive containing several directories. Using the rubyzip gem, I would like to get into a .zip archive, copy the specified directory (and its contents) and move the directory to a second .zip archive.
Ideally, I wouldn't need to extract the contents of the first .zip archive and then reseal them into the second archive. I hope there is a way to use the methods provided in rubyzip gem.
source to share
The RubyZip library needs to be 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 RubyZip versions <1.0, require 'zip/zip'
and use Zip::ZipFile
insteadZip::File
source to share
This is a bit of a brute force method (and may or may not work for your application), but you can copy the entire first zip file and then use rubyzip methods to remove everything but the target directory from the copied file.
In theory, what you are asking should be possible if you use deflated compression (in which each file is stored as an individually compressed item).
source to share