Class: Omnizip::Algorithms::Deflate64::Encoder

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/algorithms/deflate64/encoder.rb

Overview

Deflate64 encoder

Constant Summary

Constants included from Constants

Constants::BLOCK_TYPE_DYNAMIC, Constants::BLOCK_TYPE_FIXED, Constants::BLOCK_TYPE_STORED, Constants::DICTIONARY_SIZE, Constants::DISTANCE_CODES, Constants::END_OF_BLOCK, Constants::GOOD_MATCH, Constants::HASH_SHIFT, Constants::HASH_SIZE, Constants::LENGTH_CODES, Constants::LITERAL_CODES, Constants::MAX_CHAIN_LENGTH, Constants::MAX_DISTANCE, Constants::MAX_DISTANCE_CODE_LENGTH, Constants::MAX_LAZY_MATCH, Constants::MAX_LITERAL_CODE_LENGTH, Constants::MAX_MATCH_LENGTH, Constants::MIN_MATCH_LENGTH, Constants::NICE_MATCH

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_stream, options = {}) ⇒ Encoder

Returns a new instance of Encoder.



14
15
16
17
18
19
20
# File 'lib/omnizip/algorithms/deflate64/encoder.rb', line 14

def initialize(output_stream, options = {})
  @output_stream = output_stream
  @window_size = options[:window_size] || DICTIONARY_SIZE
  @compression_level = options[:level] || 6
  @lz77_encoder = LZ77Encoder.new(@window_size)
  @huffman = HuffmanCoder.new
end

Instance Attribute Details

#window_sizeObject (readonly)

Returns the value of attribute window_size.



12
13
14
# File 'lib/omnizip/algorithms/deflate64/encoder.rb', line 12

def window_size
  @window_size
end

Instance Method Details

#compress(input_stream) ⇒ Object

Compress input stream to output stream

Parameters:

  • input_stream (IO)

    Input data stream



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/omnizip/algorithms/deflate64/encoder.rb', line 25

def compress(input_stream)
  data = input_stream.read

  # Step 1: LZ77 compression with 64KB window
  tokens = @lz77_encoder.find_matches(data)

  # Step 2: Huffman coding
  compressed = @huffman.encode(tokens)

  # Step 3: Serialize trees and write to output
  output = serialize_with_trees(
    compressed,
    @huffman.literal_tree,
    @huffman.distance_tree,
  )

  @output_stream.write(output)
end