rubyzip:打开zip,临时修改,发送给客户端

rubyzip: open zip, modify it temporary, send to client

我想临时修改一个zip文件并将修改后的文件发送给客户端。

现在我创建一个文件流并发送它:

  require 'zip'
  zip_stream = Zip::OutputStream.write_buffer do |zip|
    zip.put_next_entry 'new_folder/file'
    zip.print "some text"
  end

  zip_stream.rewind
  send_data zip_stream.read, type: 'application/zip', disposition: 'attachment', filename: 'thing.zip'

我不明白如何打开文件系统中的现有 zip 并将其他文件放入其中并在不保存的情况下将其发送到磁盘。

你能给我一个提示吗?

勾选这个https://github.com/rubyzip/rubyzip

require 'rubygems'
require 'zip'

folder = "Users/me/Desktop/stuff_to_zip"
input_filenames = ['image.jpg', 'description.txt', 'stats.csv']

zipfile_name = "/Users/me/Desktop/archive.zip"

Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
  input_filenames.each do |filename|
    # Two arguments:
    # - The name of the file as it will appear in the archive
    # - The original file, including the path to find it
    zipfile.add(filename, File.join(folder, filename))
  end
  zipfile.get_output_stream("myFile") { |f| f.write "myFile contains just this" }
end

最后我是这样的:

require 'zip'
zip_stream = Zip::OutputStream.write_buffer do |new_zip|

 existing_zip = Zip::File.open('existing.zip')
 existing_zip.entries.each do |e|
   new_zip.put_next_entry(e.name)
   new_zip.write e.get_input_stream.read
 end

 new_zip.put_next_entry 'new_file'
 new_zip.print "text"
end