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(**kwargs_for_streamer_new) {|SizeEstimator| ... } ⇒ Integer

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

Parameters:

Yields:

Returns:

  • (Integer)

    the size of the resulting archive, in bytes


26
27
28
29
30
31
# 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

Instance Method Details

#add_deflated_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


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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:) ⇒ Object

Add an empty directory to the archive.

Parameters:

  • dirname (String)

    the name of the directory

Returns:

  • self


80
81
82
83
# 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) ⇒ Object

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

data descriptor to specify size

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

Returns:

  • self


40
41
42
43
44
45
46
47
48
49
50
# 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