Class: Cabriolet::SZDD::Compressor
- Inherits:
-
Object
- Object
- Cabriolet::SZDD::Compressor
- Defined in:
- lib/cabriolet/szdd/compressor.rb
Overview
Compressor creates SZDD compressed files
SZDD files wrap LZSS-compressed data with a header containing metadata about the original file. The compressor supports both NORMAL (used by MS-DOS EXPAND.EXE) and QBASIC formats.
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 SZDD format.
-
#compress_data(data, output_file, **options) ⇒ Integer
Compress data from memory to SZDD format.
-
#initialize(io_system = nil, algorithm_factory = nil) ⇒ Compressor
constructor
Initialize a new SZDD compressor.
Constructor Details
#initialize(io_system = nil, algorithm_factory = nil) ⇒ Compressor
Initialize a new SZDD compressor
18 19 20 21 |
# File 'lib/cabriolet/szdd/compressor.rb', line 18 def initialize(io_system = nil, algorithm_factory = nil) @io_system = io_system || System::IOSystem.new @algorithm_factory = algorithm_factory || Cabriolet.algorithm_factory end |
Instance Attribute Details
#io_system ⇒ Object (readonly)
Returns the value of attribute io_system.
11 12 13 |
# File 'lib/cabriolet/szdd/compressor.rb', line 11 def io_system @io_system end |
Instance Method Details
#compress(input_file, output_file, **options) ⇒ Integer
Compress a file to SZDD format
34 35 36 37 38 39 40 41 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 81 |
# File 'lib/cabriolet/szdd/compressor.rb', line 34 def compress(input_file, output_file, **) format = .fetch(:format, :normal) missing_char = [:missing_char] validate_format(format) validate_missing_char(missing_char) if missing_char 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, format, input_size, missing_char, ) # Compress data using LZSS lzss_mode = if format == :normal Compressors::LZSS::MODE_EXPAND else Compressors::LZSS::MODE_QBASIC end compressor = @algorithm_factory.create( :lzss, :compressor, @io_system, input_handle, output_handle, 2048, mode: lzss_mode, ) compressed_bytes = compressor.compress 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 SZDD format
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/cabriolet/szdd/compressor.rb', line 94 def compress_data(data, output_file, **) format = .fetch(:format, :normal) missing_char = [:missing_char] validate_format(format) validate_missing_char(missing_char) if missing_char 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, format, data.bytesize, missing_char, ) # Compress data using LZSS compressor = @algorithm_factory.create( :lzss, :compressor, @io_system, input_handle, output_handle, 2048, mode: if format == :normal Compressors::LZSS::MODE_EXPAND else Compressors::LZSS::MODE_QBASIC end, ) compressed_bytes = compressor.compress header_bytes + compressed_bytes ensure @io_system.close(output_handle) if output_handle end end |