Class: ZipKit::SizeEstimator

Inherits:
Object
  • Object
show all
Defined in:
lib/zip_kit/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) {|estimator| ... } ⇒ 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:

Yield Parameters:

Returns:

  • (Integer)

    the size of the resulting archive, in bytes



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

def self.estimate(**kwargs_for_streamer_new)
  streamer = ZipKit::Streamer.new(ZipKit::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 (Integer)

    size of the uncompressed entry

  • compressed_size (Integer)

    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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zip_kit/size_estimator.rb', line 62

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



82
83
84
85
# File 'lib/zip_kit/size_estimator.rb', line 82

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 (Integer)

    size of the uncompressed entry

  • use_data_descriptor (Boolean) (defaults to: false)

    whether the entry uses a postfix

Returns:

  • self



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/zip_kit/size_estimator.rb', line 42

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