Today I Learned

A JetRockets project

How to create zip files on the fly w/o Tempfile

There are many articles about how to archive files from the server and send a zip-file to a client without persisting it on the server. But usually they don’t literally do it, because they use temporary files.

There is a simple way to do it without creating any file though. You just have to put files directly to Zip::OutputStream and then read from it. Btw pay attention: you must rewind the stream before reading it.

# some files objects
def download(files)
  zip_stream = Zip::OutputStream.write_buffer do |zip|
    files.each.with_index(1) do |file, index|
     # file name must be uniq in archive
  # important - rewind the steam
            type: 'application/zip', 
            disposition: 'attachment', 
            filename: ''
Looking for help? Each developer at JetRockets has years of experience working with Ruby applications of all types and sizes. Contact us and find out how we can help you.