Changes
- When your Rails application offers downloading a bunch of files as ZIP archive, you basically have two options:
- 1. Write a ZIP file to disk and send it as a download to the user.
- 2. Generate a ZIP archive on the fly while streaming it in chunks to the user.
- This card is about option 2, and it is actually fairly easy to set up.
- We are using this to generate ZIP archives with _lots of_ files (500k+) on the fly, and it works like a charm.
- ## Why stream downloads?
- Offering downloads of large archives can be cumbersome:
- * It takes time to build the ZIP file. Users can not start their download immediately.
- * Or you have to prepare it up front, and can only offer download links once that has happened.
- * For one-off downloads, generating a large file is unnecessary, and you have to take care of removing it once it is no longer required.
- When you generate them on the fly, downloads start immediately, and you can add contents while the user is downloading.
- ## How to do it
-The excellent [zip_tricks](https://github.com/WeTransfer/zip_tricks) gem helps you out, and is not too difficult to integrate.- +The excellent [zip_kit](https://github.com/julik/zip_kit) gem helps you out, and is not too difficult to integrate.
- +It is the successor of [zip_tricks](https://github.com/WeTransfer/zip_tricks) by the same author, whose last release dates from 2021 (see "Migrating from ZipTricks" below).
- +
- If you want to send only files from ActiveStorage or Carrierwave attachments, it's even simpler when using [zipline](https://github.com/fringd/zipline).
-## Instructions for ZipTricks- +## Instructions for ZipKit
-You want to use ZipTricks if you need some level of control, or if you are building some of the ZIP contents on the fly (like some CSV).- +You want to use ZipKit if you need some level of control, or if you are building some of the ZIP contents on the fly (like some CSV).
-1. Add `zip_tricks` to your `Gemfile` and `bundle install`.-2. In your controller, `include ZipTricks::RailsStreaming`.-3. You can then use the `zip_tricks_stream` method in controller actions to generated your contents, as described in the documentation.-4. Make sure you disable Rails middlewares that generate ETag headers for you (like Rails' [Rack::ETag](https://github.com/rack/rack/blob/main/lib/rack/etag.rb) or [Rack::SteadyETag](https://github.com/makandra/rack-steady_etag)).- +1. Add `zip_kit` to your `Gemfile` and `bundle install`.
- +2. In your controller, `include ZipKit::RailsStreaming`.
- +3. You can then use the `zip_kit_stream` method in controller actions to generate your contents, as described in the documentation. Pass it the `filename:` of the download.
- +4. Make sure no Rails middleware buffers the response, like ones that generate ETag headers for you ([Rack::ETag](https://github.com/rack/rack/blob/main/lib/rack/etag.rb) or [Rack::SteadyETag](https://github.com/makandra/rack-steady_etag)).
- * Those middlewares usually want to capture the entire response body in order to generate the ETag based on its contents. If they do that, you won't be streaming to the user.
- * Usually, you can just set your own `Last-Modified` or `ETag` header to achieve that.- + * `zip_kit_stream` takes care of the usual suspects: It sets a `Last-Modified` header, which makes ETag middlewares skip the response, keeps `Rack::Deflater` from compressing it, and disables nginx buffering through `X-Accel-Buffering: no`.
- + * If streaming still does not start, look for other middlewares that capture the body, like `Rack::MiniProfiler`.
- Example controller:
- ```ruby
- class DocumentsController < ApplicationController
- include ZipTricks::RailsStreaming- + include ZipKit::RailsStreaming
- def download
- documents = Document.all
-- fresh_when Time.current # Sets `Last-Modified` header (see above). Or say "fresh_when documents" to use the scope.-send_file_headers! filename: 'all_documents.zip' # Sets `Content-Disposition` and `Content-Type` headers.-- zip_tricks_stream do |zip|- +
- +# Also sets `Content-Disposition`, `Content-Type` and `Last-Modified` headers.
- +zip_kit_stream(filename: 'all_documents.zip') do |zip|
- documents.find_each do |document|
- filename = document.filename
- path = document.file.path
-- zip.write_stored_file(filename) do |stream|
- File.open(path, 'rb') do |source|
- IO.copy_stream(source, stream)
- end
- end
- end
- end
- end
- end
- ```
- What happens here is:
-1. `zip_tricks_stream` generates a buffer object (`zip`) can be written to from inside the block.- +1. `zip_kit_stream` sets the download headers, and generates a buffer object (`zip`) that can be written to from inside the block.
- 2. The controller action completes.
-3. Your code inside the block given to `zip_tricks_stream` runs and writes to the buffer object.-4. ZipTricks streams the contents to the user.- +3. Your code inside the block given to `zip_kit_stream` runs and writes to the buffer object.
- +4. ZipKit streams the contents to the user.
- About compression:
- * Use `write_stored_file` for files that are large or unlikely to compress significantly (like PNG, JPEG, MP4, ...)
- * Use `write_deflated_file` when adding files that compress well, like CSV, XML, or other text documents.
- +### Migrating from ZipTricks
- +
- +ZipKit keeps the API of ZipTricks 5, so migrating is mostly a search and replace:
- +
- +* Replace `zip_tricks` with `zip_kit` in your `Gemfile`, and `ZipTricks` with `ZipKit` in your code.
- +* Replace `zip_tricks_stream` with `zip_kit_stream(filename: ...)`. It sets the `Content-Disposition` and `Content-Type` headers itself, so you can remove a preceding `send_file_headers!` call.
- +* ZipKit sets some response headers in lowercase, like `x-accel-buffering`. HTTP header names are case-insensitive, but tests that compare header names case-sensitively need to be adjusted.
- +
- ## Instructions for Zipline
-Zipline uses ZipTricks under the hood and is specifically intended for streaming existing file attachments (from ActiveStorage, Carrierwave, etc.).- +Zipline uses ZipKit (or ZipTricks, before version 2) under the hood and is specifically intended for streaming existing file attachments (from ActiveStorage, Carrierwave, etc.).
- 1. Add `zipline` to your `Gemfile` and `bundle install`.
- 2. In your controller, `include Zipline`.
- 3. You can then use the `zipline` method in controller actions as described in the gem's documentation.
- Note that Zipline will set a `Last-Modified` header already, disabling any etagging middlewares and allowing streaming.
- Example controller:
- ```ruby
- class DocumentsController < ApplicationController
- include Zipline
- def download
- documents = Document.all
-- files_and_filenames = documents.find_each.lazy.map do |document|
- [document.file, document.filename]
- end
- +
- zipline(files_and_filenames, 'all_documents.zip')
- end
- end
- ```
Posted by Arne Hartherz to makandra dev (2026-11-30 15:36)