views:

401

answers:

2

I want to extract the files within a ZIP file I uploaded to my Rails app. The files within the ZIP file are going to be stored in the database.

I want to open the ZIP file in my action, without first having to save the file to a folder - I want to open the multipart/form-data stream with rubyzip.

It looks like rubyzip's ZipFile.open only takes a filename - not an IO stream.

What do I need to change within rubyzip, to allow me to open the zip file as a stream, like this:

Zip::ZipFile.open(params["zip_file"]) do |zip_file|
 ...
end

Thanks. Joerg

+1  A: 

I'm going to give you some advice that you haven't asked for.

I would strongly advise that you don't perform this operation from within your action, because it will block the Rails process associated with that HTTP request for as long as it takes to perform the extraction. Your UI for that user will become unresponsive and if enough users do this simultaneously (you are limiting the file upload size, right?) then you've effectively got a Denial of Service attack going on against your application.

  • Initiate the extraction as an asynchronous background job from within your action.
John Topley
Yeah I normally do it asynchronously, but in this particular case the file is not allowed to be saved first. It's an internal system with only a handful of people having access to it. Thanks, though.
Joerg
A: 

Using

Zip::ZipFile.open(params["zip_file"].path) do |zip_file|
 ...
end

should work.

Daniel Huckstep