send_zip
This gem enables Rails(or any Rack based app) to send the files with being zipped dynamically. Features are:
- Downloading starts immediately, so you don't need to wait until files are compeltely zipped. It's nice for downloading large sized files.
- Can zip a file, files, or folder. Zipped folder keeps the directory structure.
- Can set the encoding of file name in the zip. Now only UTF-8 and Shift-JIS are supported.
This gem is based on zipping.
Getting Started
Add the following line to your Gemfile:
gem 'send_zip'
Usage
In your controller, include SendZip
class DownloadController < ApplicationController
include SendZip
...
end
then you can use send_zip
with the argument of file path.
def download
send_zip '/path/to/file'
end
You can set an array of files
send_zip ['/path/to/file', '/path/to/another']
or folder
send_zip '/path/to/folder'
Options
Some options are available like send_file
.
send_zip '/path/to/file', filename: 'foo.zip', buffer_size: 1024
Option | Description | Default |
---|---|---|
:filename |
Filename | File.basename(path) + '.zip' |
:type |
MIME-Type | 'application/zip' |
:disposition |
'attachment' | |
:buffer_size |
1048576 | |
:encoding |
Filename encoding type. Now :utf8 and :shift_jis | :utf8 |
Notice
WEBrick is not compatible with HTTP Streaming. Use Puma, Unicorn, thin, etc. I recommend puma.
Rails Sample
Rails4 with Puma. Rails sample
Copyright Nekojarashi Inc.