Class: ZipTricks::SizeEstimator

Inherits:
Object
  • Object
show all
Defined in:
lib/zip_tricks/size_estimator.rb

Overview

Helps to estimate archive sizes

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.estimate {|SizeEstimator| ... } ⇒ Fixnum

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_compressed_entry(filename: "family.tif", uncompressed_size: 89281911, compressed_size: 121908)
end

Yields:

Returns:

  • (Fixnum)

    the size of the resulting archive, in bytes



28
29
30
31
32
# File 'lib/zip_tricks/size_estimator.rb', line 28

def self.estimate
  output_io = ZipTricks::WriteAndTell.new(ZipTricks::NullWriter)
  DetailStreamer.open(output_io) { |zip| yield(new(zip)) }
  output_io.tell
end

Instance Method Details

#add_compressed_entry(filename:, uncompressed_size:, compressed_size:, use_data_descriptor: false) ⇒ Object

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

Parameters:

  • filename (String)

    the name of the file (filenames are variable-width in the ZIP)

  • uncompressed_size (Fixnum)

    size of the uncompressed entry

  • compressed_size (Fixnum)

    size of the compressed entry

  • use_data_descriptor (Boolean) (defaults to: false)

    whether the entry uses a postfix data descriptor to specify size

Returns:

  • self



56
57
58
59
60
61
62
63
# File 'lib/zip_tricks/size_estimator.rb', line 56

def add_compressed_entry(filename:, uncompressed_size:, compressed_size:, use_data_descriptor: false)
  udd = !!use_data_descriptor
  @streamer.add_file_and_write_local_header(filename: filename, crc32: 0, storage_mode: 8,
    compressed_size: compressed_size, uncompressed_size: uncompressed_size, use_data_descriptor: udd)
  @streamer.simulate_write(compressed_size)
  @streamer.write_data_descriptor_for_last_entry if udd
  self
end

#add_stored_entry(filename:, size:, use_data_descriptor: false) ⇒ Object

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

Parameters:

  • filename (String)

    the name of the file (filenames are variable-width in the ZIP)

  • size (Fixnum)

    size of the uncompressed entry

  • use_data_descriptor (Boolean) (defaults to: false)

    whether the entry uses a postfix data descriptor to specify size

Returns:

  • self



40
41
42
43
44
45
46
47
# File 'lib/zip_tricks/size_estimator.rb', line 40

def add_stored_entry(filename:, size:, use_data_descriptor: false)
  udd = !!use_data_descriptor
  @streamer.add_file_and_write_local_header(filename: filename, crc32: 0, storage_mode: 0,
    compressed_size: size, uncompressed_size: size, use_data_descriptor: udd)
  @streamer.simulate_write(size)
  @streamer.write_data_descriptor_for_last_entry if udd
  self
end