Class: Omnizip::Algorithms::Deflate::Encoder

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

Overview

Deflate encoder using Zlib

This class wraps Ruby's Zlib::Deflate to provide Deflate compression following the established Omnizip architecture.

Constant Summary

Constants included from Constants

Constants::BEST_COMPRESSION, Constants::BEST_SPEED, Constants::BUFFER_SIZE, Constants::DEFAULT_COMPRESSION, Constants::DEFAULT_STRATEGY, Constants::FILTERED, Constants::FIXED, Constants::HUFFMAN_ONLY, Constants::NO_COMPRESSION, Constants::RLE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize encoder

Parameters:

  • output_stream (IO)

    Output stream for compressed data

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

    Encoder options

Options Hash (options):

  • :level (Integer)

    Compression level (0-9)

  • :strategy (Integer)

    Compression strategy

  • :window_bits (Integer)

    Window size (8-15)



24
25
26
27
28
29
30
# File 'lib/omnizip/algorithms/deflate/encoder.rb', line 24

def initialize(output_stream, options = {})
  @output_stream = output_stream
  @options = options
  @level = options[:level] || DEFAULT_COMPRESSION
  @strategy = options[:strategy] || DEFAULT_STRATEGY
  @window_bits = options[:window_bits] || 15
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/omnizip/algorithms/deflate/encoder.rb', line 15

def options
  @options
end

#output_streamObject (readonly)

Returns the value of attribute output_stream.



15
16
17
# File 'lib/omnizip/algorithms/deflate/encoder.rb', line 15

def output_stream
  @output_stream
end

Instance Method Details

#encode_stream(data) ⇒ void

This method returns an undefined value.

Encode data stream

Parameters:

  • data (String)

    Data to compress



36
37
38
39
40
41
# File 'lib/omnizip/algorithms/deflate/encoder.rb', line 36

def encode_stream(data)
  deflater = Zlib::Deflate.new(@level, @window_bits, 9, @strategy)
  compressed = deflater.deflate(data, Zlib::FINISH)
  deflater.close
  @output_stream.write(compressed)
end