Class: IOStreams::Zip::Writer
- Defined in:
- lib/io_streams/zip/writer.rb
Instance Attribute Summary
Attributes inherited from Writer
Class Method Summary collapse
-
.file(file_name, zip_file_name: nil, entry_file_name: zip_file_name) ⇒ Object
When writing to a file, default the entry name within the zip to the file name without the ‘.zip` extension, unless an entry name was explicitly supplied.
-
.stream(output_stream, zip_file_name: nil, entry_file_name: zip_file_name) ⇒ Object
Write a single file in Zip format to the supplied output stream.
Methods inherited from Writer
Constructor Details
This class inherits a constructor from IOStreams::Writer
Class Method Details
.file(file_name, zip_file_name: nil, entry_file_name: zip_file_name) ⇒ Object
When writing to a file, default the entry name within the zip to the file name without the ‘.zip` extension, unless an entry name was explicitly supplied.
6 7 8 9 10 |
# File 'lib/io_streams/zip/writer.rb', line 6 def self.file(file_name, zip_file_name: nil, entry_file_name: zip_file_name, &) entry_file_name = file_name.to_s[0..-5] if entry_file_name.nil? && file_name.to_s =~ /\.zip\z/i super(file_name, entry_file_name: entry_file_name, &) end |
.stream(output_stream, zip_file_name: nil, entry_file_name: zip_file_name) ⇒ Object
Write a single file in Zip format to the supplied output stream
Parameters
output_stream [IO]
Output stream to write to
entry_file_name: [String]
Name of the file entry within the Zip file.
Default: "file"
The stream supplied to the block only responds to #write
Note:
This writer uses `zip_kit` rather than `rubyzip` on purpose. `rubyzip`'s
`Zip::OutputStream` requires a seekable output: it seeks back to rewrite each
entry's local header with the CRC and sizes once the entry is finished. That
means it cannot write directly to a non-seekable destination (S3, SFTP, HTTP,
a socket); the output would first have to be spooled to a temporary file and
then copied across. `zip_kit` streams to non-seekable outputs by emitting
data descriptors instead, so we can write straight to the output stream and
avoid the temp file round-trip.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/io_streams/zip/writer.rb', line 33 def self.stream(output_stream, zip_file_name: nil, entry_file_name: zip_file_name) entry_file_name ||= "file" Utils.load_soft_dependency("zip_kit", "Zip") unless defined?(ZipKit::Streamer) result = nil ZipKit::Streamer.open(output_stream) do |zip| zip.write_deflated_file(entry_file_name) { |io| result = yield(io) } end result end |