module Zipline

class MyController < ApplicationController

include Zipline
def index
  users = User.all
  files = users.map{ |user| [user.avatar, "#{user.username}.png", modification_time: 1.day.ago] }
  zipline(files, 'avatars.zip')
end

end

this class acts as a streaming body for rails initialize it with an array of the files you want to zip

Constants

VERSION

Public Instance Methods

zipline(files, zipname = 'zipline.zip') click to toggle source
# File lib/zipline.rb, line 14
def zipline(files, zipname = 'zipline.zip')
  zip_generator = ZipGenerator.new(files)
  headers['Content-Disposition'] = "attachment; filename=\"#{zipname.gsub '"', '\"'}\""
  headers['Content-Type'] = Mime::Type.lookup_by_extension('zip').to_s
  response.sending_file = true
  response.cache_control[:public] ||= false
  self.response_body = zip_generator
  self.response.headers['Last-Modified'] = Time.now.httpdate
end