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 {|SizeEstimator| ... } ⇒ Fixnum
Performs the estimate using fake archiving.
Instance Method Summary collapse
-
#add_compressed_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_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 {|SizeEstimator| ... } ⇒ Fixnum
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_compressed_entry(filename: "family.tif", uncompressed_size: 89281911, compressed_size: 121908)
end
28 29 30 31 32 |
# File 'lib/zip_tricks/size_estimator.rb', line 28 def self.estimate output_io = ZipTricks::WriteAndTell.new(ZipTricks::NullWriter) DetailStreamer.open(output_io) { |zip| yield(new(zip)) } output_io.tell end |
Instance Method Details
#add_compressed_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.
56 57 58 59 60 61 62 63 |
# File 'lib/zip_tricks/size_estimator.rb', line 56 def add_compressed_entry(filename:, uncompressed_size:, compressed_size:, use_data_descriptor: false) udd = !!use_data_descriptor @streamer.add_file_and_write_local_header(filename: filename, crc32: 0, storage_mode: 8, compressed_size: compressed_size, uncompressed_size: uncompressed_size, use_data_descriptor: udd) @streamer.simulate_write(compressed_size) @streamer.write_data_descriptor_for_last_entry if udd 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.
40 41 42 43 44 45 46 47 |
# File 'lib/zip_tricks/size_estimator.rb', line 40 def add_stored_entry(filename:, size:, use_data_descriptor: false) udd = !!use_data_descriptor @streamer.add_file_and_write_local_header(filename: filename, crc32: 0, storage_mode: 0, compressed_size: size, uncompressed_size: size, use_data_descriptor: udd) @streamer.simulate_write(size) @streamer.write_data_descriptor_for_last_entry if udd self end |