class ZipTricks::SizeEstimator

Helps to estimate archive sizes

Public Class Methods

estimate(**kwargs_for_streamer_new) { |estimator| ... } click to toggle source

Performs the estimate using fake archiving. It needs to know the sizes of the entries upfront. Usage:

expected_zip_size = SizeEstimator.estimate do | estimator |
  estimator.add_stored_entry(filename: "file.doc", size: 898291)
  estimator.add_deflated_entry(filename: "family.tif",
          uncompressed_size: 89281911, compressed_size: 121908)
end

@param kwargs_for_streamer_new Any options to pass to Streamer, see {Streamer#initialize} @return [Integer] the size of the resulting archive, in bytes @yield [SizeEstimator] the estimator

# File lib/zip_tricks/size_estimator.rb, line 26
def self.estimate(**kwargs_for_streamer_new)
  streamer = ZipTricks::Streamer.new(ZipTricks::NullWriter, **kwargs_for_streamer_new)
  estimator = new(streamer)
  yield(estimator)
  streamer.close # Returns the .tell of the contained IO
end
new(streamer) click to toggle source

Creates a new estimator with a Streamer object. Normally you should use `estimate` instead an not use this method directly.

# File lib/zip_tricks/size_estimator.rb, line 9
def initialize(streamer)
  @streamer = streamer
end

Public Instance Methods

add_deflated_entry(filename:, uncompressed_size:, compressed_size:, use_data_descriptor: false) click to toggle source

Add a fake entry to the archive, to see how big it is going to be in the end.

@param filename [String] the name of the file (filenames are variable-width in the ZIP) @param uncompressed_size [Fixnum] size of the uncompressed entry @param compressed_size [Fixnum] size of the compressed entry @param use_data_descriptor whether the entry uses a postfix data

descriptor to specify size

@return self

# File lib/zip_tricks/size_estimator.rb, line 60
def add_deflated_entry(filename:, uncompressed_size:, compressed_size:, use_data_descriptor: false)
  @streamer.add_deflated_entry(filename: filename,
                               crc32: 0,
                               compressed_size: compressed_size,
                               uncompressed_size: uncompressed_size,
                               use_data_descriptor: use_data_descriptor)

  @streamer.simulate_write(compressed_size)
  if use_data_descriptor
    @streamer.update_last_entry_and_write_data_descriptor(crc32: 0,
                                                          compressed_size: compressed_size,
                                                          uncompressed_size: uncompressed_size)
  end
  self
end
add_empty_directory_entry(dirname:) click to toggle source

Add an empty directory to the archive.

@param dirname [String] the name of the directory @return self

# File lib/zip_tricks/size_estimator.rb, line 80
def add_empty_directory_entry(dirname:)
  @streamer.add_empty_directory(dirname: dirname)
  self
end
add_stored_entry(filename:, size:, use_data_descriptor: false) click to toggle source

Add a fake entry to the archive, to see how big it is going to be in the end.

@param filename [String] the name of the file (filenames are variable-width in the ZIP) @param size [Fixnum] size of the uncompressed entry @param use_data_descriptor whether the entry uses a postfix data descriptor to specify size @return self

# File lib/zip_tricks/size_estimator.rb, line 40
def add_stored_entry(filename:, size:, use_data_descriptor: false)
  @streamer.add_stored_entry(filename: filename,
                             crc32: 0,
                             size: size,
                             use_data_descriptor: use_data_descriptor)
  @streamer.simulate_write(size)
  if use_data_descriptor
    @streamer.update_last_entry_and_write_data_descriptor(crc32: 0, compressed_size: size, uncompressed_size: size)
  end
  self
end