Reputation: 4905
I have a TempFile
object that is a zip file, and I wish to read from it as follows:
Zip::ZipFile.open_buffer(tempfile) do |zipfile|
...
end
However, when I do this, I get the following error:
Zip::ZipFile.open_buffer expects an argument of class String or IO. Found: Tempfile
I've also tried
Zip::ZipFile.open(tempfile.path) do |zipfile|
...
end
But that returns
can't dup NilClass
How can I process a temporary zip file?
Upvotes: 5
Views: 2543
Reputation: 164
I faced the same error , but after digging i found out that these zip file should be in binary
i.e, first copy them to some file in binary mode then you can unzip it using ZIP module without facing the error
sample code
#copying zip file to a new file in binary mode
filename = "empty.zip"
File.open(filename, "wb") do |empty_file|
open("#{zipfile_url}", 'rb') do |read_file|
empty_file.write(read_file.read)
end
end
#now you can open the zip file
Zip::File.open(filename) do |f|
. . .
end
Upvotes: 0
Reputation: 4905
It turns out that the temporary file was corrupted, so the
can't dup NilClass
error was as a result of trying to read the corrupted file.
Therefore the solution is to use
Zip::ZipFile.open(tempfile.path) do |zipfile|
...
end
Upvotes: 3
Reputation: 42192
See the following article http://info.michael-simons.eu/2008/01/21/using-rubyzip-to-create-zip-files-on-the-fly/ which explains how to use the more basic interface Zip::ZipOutputStream if you work with a Tempfile
Upvotes: 3