Create zip files on the fly with ruby
02:21 PMDevelopment, RubyClaudio
In a Ruby on Rails application I’m developing, client asked me to add a feature that allows users to download a full picture gallery as a single zip file.
Quite obviously, I decided to take advantage of rubyzip gem to create the compressed archive to download.
But my idea was to create the zip on the fly, directly in memory, without fill folders of my application with many zip files that I need to remove later.
Hence the idea of using Tempfile to create the file in memory.
Here’s the function I created: “download_zip“.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | def download_zip(image_list) if !image_list.blank? file_name = "pictures.zip" t = Tempfile.new("my-temp-filename-#{Time.now}") Zip::ZipOutputStream.open(t.path) do |z| image_list.each do |img| title = img.title title += ".jpg" unless title.end_with?(".jpg") z.put_next_entry(title) z.print IO.read(img.path) end end send_file t.path, :type => 'application/zip', :disposition => 'attachment', :filename => file_name t.close end end |
This is what the download_zip function does:
- takes as input a list of Image objects, where Image has at least the two properties: title and path.
- create a temporary file and open it as a ZipOutputStream.
- for each image in the list creates a new element in ZipOutputStream with the name equal to the image title and content is the image read from path.
- the close ofthe block Zip::ZipOutputStream … end will cause the new file zip will be automatically closed.
- now, the file is sent to the user, with the right mime-type set.
- the last instruction close the temporary file that will be removed from memory later by the garbage collector.
As you can see, with a few lines of code you have gotten a very clean and effective solution.
Tags: ruby, ruby on rails, zip

















Tempfile.new(“my-temp-filename-#{request.remote_ip}”)
using the client ip address to generate a unique file is flawed…
what happens if a user has more than one browser open, trying to download pictures?
also, keep in mind that in many scenarios (universities, large corporations, fastweb), large subnets access the internet using a single IP address.
a better solution would be http://raa.ruby-lang.org/project/ruby-uuid/
keep up the great stuff!
Your observation is correct. Thank you for letting me see this “unforgivable” mistake. I’ve adjusted the example using a simple but effective workaround:
Tempfile.new ( “my-temp-filename-request.remote_ip # ()”)
now becomes
Tempfile.new ( “my-temp-filename-# (Time.now)”)
so that the name is no more linked to the ip-address but rather to the download-time.
Excellent tip, I will be using this for my website! My only comment is that it is still possible for two people to request zip files at the same time (Time.now is accurate to the second), so I might either use a UUID (as Ramzi suggested), or just add a random number, like so:
Tempfile.new(“my-temp-filename-#{Time.now.to_s + rand(9999).to_s}”
Also, since the files exist only in memory, and therefore likely only last a short amount of time, Time.now provides very little entropy. Just a thought!
You can also go to open zip file to open your zip files online. I hope it would be a great help.
Claudio,
I’ve got this error when using your code at line 12:
wrong argument type String (expected Zip::Archive)
11. t = Tempfile.new(“my-temp-filename-#{Time.now}”)
12. Zip::ZipOutputStream.open(t.path) do |z|
What am i doing wrong?
Hi Ricardo,
I’ve tried to reproduce your error without success.
May be you’ve not included zip gems in your controller?
Try with with:
require ‘zip/zip’
require ‘zip/zipfilesystem’
I’m getting all sorts of errors for this, namely that the images come out in the compressed folder corrupted, with bad filetypes and defintions.
All of the zip creation and filenaming goes well, but when the file has been downloaded, the files just refuse to show due to either being corrupted (through ASCII conversion) or have bogus Huffman definitions which flag them as Qt-PICT exploits.
Anyone else run into this issue?
Hey Steve, I am running into the same problem. I am on OS X running Ruby 1.9.1 .. whenever i try to unzip the resulting file Unarchiver tells me there is an error decrunching the file…
how to unzip an image file in rails3
This worked great for me under 1.8.7 but after upgrading to Rails 3 and Ruby 1.9.2 I started running into the same problems as Steve. I suggest you give http://zipruby.rubyforge.org/ a try if you are having issues.
Hi,
thanks for the tip about creating a zip file…
But where did you see that Tempfile was creating a file in memory ?
From the doc you linked :
“Creates a temporary file of mode 0600 in the temporary directory”
Otherwise it should fit your bill,
Regards
Thanks for the article but I think it would be even better to call
Time.now.to_i
this gives you the timestamp and will be an integer. The Time.now you use could contain all kind of strange characters (e.g. a slash or dots) which may pose some problems on certain file systems, don’t you think?
With Ruby 1.9.2 I found using z.write instead of z.print fixed my zip corruption problem.
Tahnks for the interesting article!
this didn’t work for me on 1.8.7 in combination with heroku – i get an empty file – but when i closed the file and read it as data and streamed it as data with send_data than it does work.
Zip::ZipOutputStream.open(file.path) do |z|
files.each do |wood, report|
title = wood.abbreviation+”.txt”
z.put_next_entry(title)
z.write report
end
end
file.close
file = File.open(file.path, “r”)
data = file.read()
send_data data, :type => ‘application/zip’, :disposition => ‘attachment’, :filename => file_name
Dude.. I am not much into reading, but somehow I got to read lots of articles on your website. Its amazing how interesting it is for me to visit you very often.
Very fantastic visual appeal on this site, I’d value it 10 10.
I’ve been using your code for an application coded in Rails 2.0.2 (Ruby 1.8.6).
We had an issue that occurred very rarely: sometimes, during the send_file, the file was not found.
After much investigation, we understood why: the send_file has an option (:stream) which defaults to true. If set to true, the file is read and sent on the fly. The problem is that in your code, you close and delete the temporary file just after the send_file.
In our case, and maybe because we used an old version of Rails, the file was sometimes sent after being deleted (thus raising an error).
The solution is just to add :stream => false:
send_file t.path, :type => ‘application/zip’,
:disposition => ‘attachment’,
:filename => file_name,
:stream => false
t.close
With this option, the file is read BEFORE being sent, and then (and only then) closed.
This option disappeared in Rails 3 (or around it) because it is handled in another way that I haven’t investigated.
I’d suggest you to add this for people who, like me, would come to your blog and experience the same problem
[...] know that I can do streaming generation of zip files to the filesystemk using ZipOutputStream as here. I also know that I can do streaming output from a rails controller by setting response_body to a [...]
A few things to note. Tempfile doesn’t create file in memory (unless your tmp directory is in something like tmpfs). Also you do not need to put Time.now or anything of the sort in the first parameter to the constructor. Tempfile.new takes a “basename” which is the prefix used to create the temporary filename.. the underlying make_tmpname method already mixes in the time and a random value.