Class: ZipTricks::SizeEstimator
- Inherits:
-
Object
- Object
- ZipTricks::SizeEstimator
- Defined in:
- lib/zip_tricks/size_estimator.rb
Overview
Helps to estimate archive sizes
Class Method Summary collapse
-
.estimate(**kwargs_for_streamer_new) {|SizeEstimator| ... } ⇒ 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) {|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
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.
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.
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
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 |