Created
January 10, 2022 07:56
-
-
Save s1monj/746cc5add6a77646610f715282767cf2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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