Skip to content

Instantly share code, notes, and snippets.

@s1monj
Created January 10, 2022 07:56
Show Gist options
  • Save s1monj/746cc5add6a77646610f715282767cf2 to your computer and use it in GitHub Desktop.
Save s1monj/746cc5add6a77646610f715282767cf2 to your computer and use it in GitHub Desktop.
class MyApp < Sinatra::Base
FILE_PATHS = [
'document.pdf',
'movie.mov'
]
get "/zip" do
body = ZipStreamer.make_streaming_body(file_paths: file_paths)
headers = {'Transfer-Encoding' => 'chunked', 'Content-Disposition' => 'attachment; filename="myzip.zip"'}
[200,headers,body]
end
end
class ZipStreamer
def self.make_streaming_body(file_paths: )
ZipTricks::RackBody.new do |file|
file_paths.each do |file_path|
zip.write_stored_file(file_path) do |sink|
File.open(file_path, 'rb'){
|source| IO.copy_stream(source, sink)
}
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment