Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify a zip file in memory only?

I have a Ruby application, and I need to modify an existing zip file.

I want to build the zip file in memory and stream back the bytes without ever writing the file to the filesystem. If I end up hosting this on Heroku I don't think I can write to the filesystem. Does anyone know of a way to do that?

I looked at Zip::ZipFile but it looks like it always wants to write to the filesystem. I figured being "based on the java implementation" I would be able to just get the compressed file's bytes, which you can do in java, but I don't see a way to do that.


Edit:

What I am asking is basically the same as this, but for Ruby instead of Python: Function to create in-memory zip file and return as http response

like image 526
CodingWithSpike Avatar asked Dec 03 '10 22:12

CodingWithSpike


1 Answers

had same issue, got to get it work by closing the file and reading the data and streaming it as send_data

then found another library that works fine on heroku and can handle with in-memory buffers: it's zipruby (not rubyzip).

buffer = ''
Zip::Archive.open_buffer(buffer, Zip::CREATE) do |archive|
  files.each do |wood, report|
    title = wood.abbreviation+".txt"
    archive.add_buffer(title, report);
  end
end
file_name = "dimter_#{@offer.customerName}_#{Time.now.strftime("%m%d%Y_%H%M")}.zip"
send_data buffer, :type => 'application/zip', :disposition => 'attachment', :filename => file_name
like image 195
koen Avatar answered Sep 23 '22 18:09

koen