Class: Cabriolet::KWAJ::Compressor
- Inherits:
-
Object
- Object
- Cabriolet::KWAJ::Compressor
- Defined in:
- lib/cabriolet/kwaj/compressor.rb
Overview
Compressor creates KWAJ compressed files
KWAJ files support multiple compression methods:
-
NONE: Direct copy
-
XOR: XOR with 0xFF “encryption”
-
SZDD: LZSS compression
-
MSZIP: DEFLATE compression
KWAJ headers contain optional fields controlled by flag bits:
-
Uncompressed length (4 bytes)
-
Filename (up to 9 bytes, null-terminated)
-
File extension (up to 4 bytes, null-terminated)
-
Extra data (2 bytes length + variable data)
Instance Attribute Summary collapse
-
#io_system ⇒ Object
readonly
Returns the value of attribute io_system.
Instance Method Summary collapse
-
#compress(input_file, output_file, **options) ⇒ Integer
Compress a file to KWAJ format.
-
#compress_data(data, output_file, **options) ⇒ Integer
Compress data from memory to KWAJ format.
-
#initialize(io_system = nil) ⇒ Compressor
constructor
Initialize a new KWAJ compressor.
Constructor Details
#initialize(io_system = nil) ⇒ Compressor
Initialize a new KWAJ compressor
25 26 27 |
# File 'lib/cabriolet/kwaj/compressor.rb', line 25 def initialize(io_system = nil) @io_system = io_system || System::IOSystem.new end |
Instance Attribute Details
#io_system ⇒ Object (readonly)
Returns the value of attribute io_system.
19 20 21 |
# File 'lib/cabriolet/kwaj/compressor.rb', line 19 def io_system @io_system end |
Instance Method Details
#compress(input_file, output_file, **options) ⇒ Integer
Compress a file to KWAJ format
42 43 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 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cabriolet/kwaj/compressor.rb', line 42 def compress(input_file, output_file, **) compression_type = .fetch(:compression, :szdd) include_length = .fetch(:include_length, false) filename = [:filename] extra_data = [:extra_data] validate_compression_type(compression_type) input_handle = @io_system.open(input_file, Constants::MODE_READ) output_handle = @io_system.open(output_file, Constants::MODE_WRITE) begin # Get input size input_size = @io_system.seek(input_handle, 0, Constants::SEEK_END) @io_system.seek(input_handle, 0, Constants::SEEK_START) # Write header header_bytes = write_header( output_handle, compression_type, input_size, include_length, filename, extra_data, ) # Compress data compressed_bytes = compress_data_stream( compression_type, input_handle, output_handle, ) header_bytes + compressed_bytes ensure @io_system.close(input_handle) if input_handle @io_system.close(output_handle) if output_handle end end |
#compress_data(data, output_file, **options) ⇒ Integer
Compress data from memory to KWAJ format
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 120 121 122 123 124 125 126 127 128 |
# File 'lib/cabriolet/kwaj/compressor.rb', line 95 def compress_data(data, output_file, **) compression_type = .fetch(:compression, :szdd) include_length = .fetch(:include_length, false) filename = [:filename] extra_data = [:extra_data] validate_compression_type(compression_type) input_handle = System::MemoryHandle.new(data) output_handle = @io_system.open(output_file, Constants::MODE_WRITE) begin # Write header header_bytes = write_header( output_handle, compression_type, data.bytesize, include_length, filename, extra_data, ) # Compress data compressed_bytes = compress_data_stream( compression_type, input_handle, output_handle, ) header_bytes + compressed_bytes ensure @io_system.close(output_handle) if output_handle end end |