Class: ZipKit::Streamer::Heuristic

Inherits:
Writable
  • Object
show all
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

Methods included from WriteShovel

#write

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
# File 'lib/zip_kit/streamer/heuristic.rb', line 19

def initialize(streamer, filename, **write_file_options)
  @streamer = streamer
  @filename = filename
  @write_file_options = write_file_options

  @buf = StringIO.new.binmode
  @deflater = ::Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, -::Zlib::MAX_WBITS)
  @bytes_deflated = 0

  @winner = nil
end

Instance Method Details

#<<(bytes) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/zip_kit/streamer/heuristic.rb', line 31

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

#closeObject



42
43
44
45
# File 'lib/zip_kit/streamer/heuristic.rb', line 42

def close
  decide unless @winner
  @winner.close
end