Class: Cabriolet::KWAJ::CommandHandler

Inherits:
Commands::BaseCommandHandler show all
Defined in:
lib/cabriolet/kwaj/command_handler.rb

Overview

Command handler for KWAJ compressed format

This handler implements the unified command interface for KWAJ files, wrapping the existing KWAJ::Decompressor and KWAJ::Compressor classes.

Instance Method Summary collapse

Methods inherited from Commands::BaseCommandHandler

#initialize

Constructor Details

This class inherits a constructor from Cabriolet::Commands::BaseCommandHandler

Instance Method Details

#create(output, files = [], options = {}) ⇒ void

This method returns an undefined value.

Create KWAJ compressed file

Compresses a file using KWAJ compression.

Parameters:

  • output (String) —

    Output KWAJ file path

  • files (Array<String>) (defaults to: []) —

    Input file (single file for KWAJ)

  • options (Hash) (defaults to: {}) —

    Additional options

Options Hash (options):

  • :compression (String) —

    Compression method (:none, :xor, :szdd, :mszip)

  • :include_length (Boolean) —

    Include uncompressed length

  • :filename (String) —

    Original filename to embed

  • :extra_data (String) —

    Extra data to include

Raises:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cabriolet/kwaj/command_handler.rb', line 84

def create(output, files = [], options = {})
  raise ArgumentError, "No file specified" if files.empty?

  if files.size > 1
    raise ArgumentError,
          "KWAJ format supports only one file at a time"
  end

  file = files.first
  unless File.exist?(file)
    raise ArgumentError,
          "File does not exist: #{file}"
  end

  compression = parse_compression_option(options[:compression])
  compress_options = { compression: compression }

  compress_options[:include_length] = true if options[:include_length]
  compress_options[:filename] = options[:filename] if options[:filename]
  if options[:extra_data]
    compress_options[:extra_data] =
      options[:extra_data]
  end

  # Auto-generate output name if not provided
  if output.nil?
    output = "#{file}.kwj"
  end

  compressor = Compressor.new

  puts "Compressing #{file} -> #{output} (#{compression} compression)" if verbose?
  bytes = compressor.compress(file, output, **compress_options)

  puts "Compressed #{file} to #{output} (#{bytes} bytes, #{compression} compression)"
end

#extract(file, output_dir = nil, options = {}) ⇒ void

This method returns an undefined value.

Extract KWAJ compressed file

Extracts/decompresses the KWAJ file to its original form. Auto-detects output filename if not specified.

Parameters:

  • file (String) —

    Path to the KWAJ file

  • output_dir (String) (defaults to: nil) —

    Output directory (not typically used for KWAJ)

  • options (Hash) (defaults to: {}) —

    Additional options

Options Hash (options):

  • :output (String) —

    Output file path



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cabriolet/kwaj/command_handler.rb', line 44

def extract(file, output_dir = nil, options = {})
  validate_file_exists(file)

  output = options[:output]

  # Auto-detect output name if not provided
  if output.nil? && output_dir.nil?
    output = auto_output_filename(file)
  end

  # If output_dir is specified, ensure it exists and construct output path
  if output.nil? && output_dir
    output_dir = ensure_output_dir(output_dir)
    base_name = File.basename(file, ".*")
    output = File.join(output_dir, base_name)
  end

  decompressor = Decompressor.new
  header = decompressor.open(file)

  puts "Extracting #{file} -> #{output}" if verbose?
  bytes = decompressor.extract(header, file, output)
  decompressor.close(header)

  puts "Extracted #{file} to #{output} (#{bytes} bytes)"
end

#info(file, _options = {}) ⇒ void

This method returns an undefined value.

Display detailed KWAJ file information

Parameters:

  • file (String) —

    Path to the KWAJ file

  • options (Hash) —

    Additional options (unused)



126
127
128
129
130
131
132
133
134
135
# File 'lib/cabriolet/kwaj/command_handler.rb', line 126

def info(file, _options = {})
  validate_file_exists(file)

  decompressor = Decompressor.new
  header = decompressor.open(file)

  display_kwaj_info(header, file)

  decompressor.close(header)
end

#list(file, _options = {}) ⇒ void

This method returns an undefined value.

List KWAJ file information

For KWAJ files, list displays detailed file information rather than a file listing (single file archive).

Parameters:

  • file (String) —

    Path to the KWAJ file

  • options (Hash) —

    Additional options (unused)



23
24
25
26
27
28
29
30
31
32
# File 'lib/cabriolet/kwaj/command_handler.rb', line 23

def list(file, _options = {})
  validate_file_exists(file)

  decompressor = Decompressor.new
  header = decompressor.open(file)

  display_kwaj_info(header, file)

  decompressor.close(header)
end

#test(file, _options = {}) ⇒ void

This method returns an undefined value.

Test KWAJ file integrity

Verifies the KWAJ file structure.

Parameters:

  • file (String) —

    Path to the KWAJ file

  • options (Hash) —

    Additional options (unused)



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/cabriolet/kwaj/command_handler.rb', line 144

def test(file, _options = {})
  validate_file_exists(file)

  decompressor = Decompressor.new
  header = decompressor.open(file)

  puts "Testing #{file}..."
  # TODO: Implement full integrity testing
  puts "OK: KWAJ file structure is valid"
  puts "Compression: #{header.compression_name}"
  puts "Data offset: #{header.data_offset} bytes"
  puts "Uncompressed size: #{header.length || 'unknown'} bytes"

  decompressor.close(header)
end