Class: Cabriolet::CAB::Compressor
- Inherits:
-
Object
- Object
- Cabriolet::CAB::Compressor
- Defined in:
- lib/cabriolet/cab/compressor.rb
Overview
Compressor creates CAB files from source files rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#cabinet_index ⇒ Object
readonly
Returns the value of attribute cabinet_index.
-
#compression ⇒ Object
readonly
Returns the value of attribute compression.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#io_system ⇒ Object
readonly
Returns the value of attribute io_system.
-
#set_id ⇒ Object
readonly
Returns the value of attribute set_id.
-
#workers ⇒ Object
readonly
Returns the value of attribute workers.
Instance Method Summary collapse
-
#add_file(source_path, cab_path = nil) ⇒ void
Add a file to the cabinet.
-
#generate(output_file, **options) ⇒ Integer
Generate the cabinet file.
-
#initialize(io_system = nil, algorithm_factory = nil, workers: 1) ⇒ Compressor
constructor
Initialize a new compressor.
Constructor Details
#initialize(io_system = nil, algorithm_factory = nil, workers: 1) ⇒ Compressor
Initialize a new compressor
19 20 21 22 23 24 25 26 27 |
# File 'lib/cabriolet/cab/compressor.rb', line 19 def initialize(io_system = nil, algorithm_factory = nil, workers: 1) @io_system = io_system || System::IOSystem.new @algorithm_factory = algorithm_factory || Cabriolet.algorithm_factory @files = [] @compression = :mszip @set_id = rand(0xFFFF) @cabinet_index = 0 @workers = workers end |
Instance Attribute Details
#cabinet_index ⇒ Object (readonly)
Returns the value of attribute cabinet_index.
11 12 13 |
# File 'lib/cabriolet/cab/compressor.rb', line 11 def cabinet_index @cabinet_index end |
#compression ⇒ Object (readonly)
Returns the value of attribute compression.
11 12 13 |
# File 'lib/cabriolet/cab/compressor.rb', line 11 def compression @compression end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
11 12 13 |
# File 'lib/cabriolet/cab/compressor.rb', line 11 def files @files end |
#io_system ⇒ Object (readonly)
Returns the value of attribute io_system.
11 12 13 |
# File 'lib/cabriolet/cab/compressor.rb', line 11 def io_system @io_system end |
#set_id ⇒ Object (readonly)
Returns the value of attribute set_id.
11 12 13 |
# File 'lib/cabriolet/cab/compressor.rb', line 11 def set_id @set_id end |
#workers ⇒ Object (readonly)
Returns the value of attribute workers.
11 12 13 |
# File 'lib/cabriolet/cab/compressor.rb', line 11 def workers @workers end |
Instance Method Details
#add_file(source_path, cab_path = nil) ⇒ void
This method returns an undefined value.
Add a file to the cabinet
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cabriolet/cab/compressor.rb', line 34 def add_file(source_path, cab_path = nil) unless ::File.exist?(source_path) raise ArgumentError, "File does not exist: #{source_path}" end unless ::File.file?(source_path) raise ArgumentError, "Not a file: #{source_path}" end @files << { source: source_path, cab_path: cab_path || ::File.basename(source_path), } end |
#generate(output_file, **options) ⇒ Integer
Generate the cabinet file
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/cabriolet/cab/compressor.rb', line 58 def generate(output_file, **) raise ArgumentError, "No files to compress" if @files.empty? @compression = [:compression] || @compression @set_id = [:set_id] || @set_id @cabinet_index = [:cabinet_index] || @cabinet_index # Validate and cache compression method value to avoid repeated hash lookups @compression_method = compression_type_value # Collect file information file_infos = collect_file_infos # Calculate offsets and sizes offsets = calculate_offsets(file_infos) # Compress files and collect data blocks compressed_data = compress_files(file_infos) # Write cabinet file write_cabinet(output_file, file_infos, offsets, compressed_data) end |