Class: ZipKit::Streamer::Heuristic
- Defined in:
- lib/zip_kit/streamer/heuristic.rb
Overview
Will be used to pick whether to store a file in the stored
or
deflated
mode, by compressing the first N bytes of the file and
comparing the stored and deflated data sizes. If deflate produces
a sizable compression gain for this data, it will create a deflated
file inside the ZIP archive. If the file doesn't compress well, it
will use the "stored" mode for the entry. About 128KB of the
file will be buffered to pick the appropriate storage mode. The
Heuristic will call either write_stored_file
or write_deflated_file
on the Streamer passed into it once it knows which compression
method should be applied
Constant Summary collapse
- BYTES_WRITTEN_THRESHOLD =
128 * 1024
- MINIMUM_VIABLE_COMPRESSION =
0.75
Instance Method Summary collapse
- #<<(bytes) ⇒ Object
- #close ⇒ Object
-
#initialize(streamer, filename, **write_file_options) ⇒ Heuristic
constructor
A new instance of Heuristic.
Methods included from WriteShovel
Constructor Details
#initialize(streamer, filename, **write_file_options) ⇒ Heuristic
Returns a new instance of Heuristic.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/zip_kit/streamer/heuristic.rb', line 19 def initialize(streamer, filename, **) @streamer = streamer @filename = filename @write_file_options = @buf = StringIO.new.binmode @deflater = ::Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, -::Zlib::MAX_WBITS) @bytes_deflated = 0 @winner = nil @started_closing = false end |
Instance Method Details
#<<(bytes) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/zip_kit/streamer/heuristic.rb', line 32 def <<(bytes) if @winner @winner << bytes else @buf << bytes @deflater.deflate(bytes) { |chunk| @bytes_deflated += chunk.bytesize } decide if @buf.size > BYTES_WRITTEN_THRESHOLD end self end |
#close ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/zip_kit/streamer/heuristic.rb', line 43 def close return if @started_closing @started_closing = true # started_closing because an exception may get raised inside close(), as we add an entry there decide unless @winner @winner.close end |