Class: Cabriolet::CAB::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/cab/compressor.rb

Overview

Compressor creates CAB files from source files rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_system = nil, algorithm_factory = nil, workers: 1) ⇒ Compressor

Initialize a new compressor

Parameters:

  • io_system (System::IOSystem) (defaults to: nil)

    I/O system for writing

  • algorithm_factory (AlgorithmFactory, nil) (defaults to: nil)

    Custom algorithm factory or nil for default

  • workers (Integer) (defaults to: 1)

    Number of parallel worker threads (default: 1 for sequential)



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_indexObject (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

#compressionObject (readonly)

Returns the value of attribute compression.



11
12
13
# File 'lib/cabriolet/cab/compressor.rb', line 11

def compression
  @compression
end

#filesObject (readonly)

Returns the value of attribute files.



11
12
13
# File 'lib/cabriolet/cab/compressor.rb', line 11

def files
  @files
end

#io_systemObject (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_idObject (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

#workersObject (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

Parameters:

  • source_path (String)

    Path to source file

  • cab_path (String) (defaults to: nil)

    Path within cabinet (optional)



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

Parameters:

  • output_file (String)

    Path to output CAB file

  • options (Hash)

    Options

Options Hash (**options):

  • :compression (Symbol)

    Compression type (:none, :mszip, :lzx, :quantum)

  • :set_id (Integer)

    Cabinet set ID

  • :cabinet_index (Integer)

    Cabinet index in set

Returns:

  • (Integer)

    Bytes written

Raises:



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, **options)
  raise ArgumentError, "No files to compress" if @files.empty?

  @compression = options[:compression] || @compression
  @set_id = options[:set_id] || @set_id
  @cabinet_index = options[: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