Class: ZipKit::SizeEstimator
- Inherits:
-
Object
- Object
- ZipKit::SizeEstimator
- Defined in:
- lib/zip_kit/size_estimator.rb
Overview
Helps to estimate archive sizes
Class Method Summary collapse
-
.estimate(**kwargs_for_streamer_new) {|estimator| ... } ⇒ Integer
Performs the estimate using fake archiving.
Instance Method Summary collapse
-
#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.
-
#add_empty_directory_entry(dirname:) ⇒ Object
Add an empty directory to the archive.
-
#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.
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
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.
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.
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
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 |