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.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/zip_kit/size_estimator.rb', line 68 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.
88 89 90 91 |
# File 'lib/zip_kit/size_estimator.rb', line 88 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.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/zip_kit/size_estimator.rb', line 45 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 |