Skip to content

Instantly share code, notes, and snippets.

@ddrscott
Created November 29, 2017 21:50
Show Gist options
  • Select an option

  • Save ddrscott/36aaa695320719dd0edbfdeebf3fbded to your computer and use it in GitHub Desktop.

Select an option

Save ddrscott/36aaa695320719dd0edbfdeebf3fbded to your computer and use it in GitHub Desktop.
Streaming zip file contents in Rails 4
class StreamZipController < ApplicationController
include ActionController::Live
# list of interesting file paths to include in the zip.
def paths
[]
end
def send_message
response.headers['Content-Type'] = 'text/event-stream'
Zip::OutputStream.write_buffer(response.stream) do |out|
paths.each do |path|
out.put_next_entry(path)
File.open(path, 'rb') do |f|
while (chunk = f.read(64.kilobytes))
out.write(chunk)
end
end
end
end
response.stream.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment