Class: Omnizip::Algorithms::Deflate

Inherits:
Omnizip::Algorithm show all
Defined in:
lib/omnizip/algorithms/deflate.rb,
lib/omnizip/algorithms/deflate/decoder.rb,
lib/omnizip/algorithms/deflate/encoder.rb,
lib/omnizip/algorithms/deflate/constants.rb

Overview

Deflate compression algorithm (RFC 1951)

Deflate is a widely-used lossless data compression algorithm that combines LZ77 compression with Huffman coding. It is the foundation of many popular formats including ZIP, gzip, and PNG.

The algorithm works in two phases:

  1. LZ77 compression - Identifies repeated byte sequences
  2. Huffman coding - Encodes the result using variable-length codes

This implementation uses Ruby's Zlib library which provides a well-tested, efficient implementation of the Deflate algorithm.

Deflate is particularly effective for:

  • Text files and source code
  • HTML, XML, and JSON documents
  • Files with repeated patterns
  • General-purpose compression needs

Defined Under Namespace

Modules: Constants Classes: Decoder, Encoder

Instance Attribute Summary

Attributes inherited from Omnizip::Algorithm

#filter, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Omnizip::Algorithm

inherited, #initialize, #with_filter

Constructor Details

This class inherits a constructor from Omnizip::Algorithm

Class Method Details

.metadataAlgorithmMetadata

Get algorithm metadata

Returns:

  • (AlgorithmMetadata)

    Algorithm information



52
53
54
55
56
57
58
59
# File 'lib/omnizip/algorithms/deflate.rb', line 52

def self.
  Models::AlgorithmMetadata.new.tap do |meta|
    meta.name = "deflate"
    meta.description = "Deflate compression using LZ77 and " \
                       "Huffman coding (RFC 1951)"
    meta.version = "1.0.0"
  end
end

Instance Method Details

#compress(input_stream, output_stream, options = nil) ⇒ void

This method returns an undefined value.

Compress data using Deflate algorithm

Parameters:

  • input_stream (IO)

    Input stream to compress

  • output_stream (IO)

    Output stream for compressed data

  • options (Models::CompressionOptions) (defaults to: nil)

    Compression options



67
68
69
70
71
# File 'lib/omnizip/algorithms/deflate.rb', line 67

def compress(input_stream, output_stream, options = nil)
  input_data = input_stream.read
  encoder = Encoder.new(output_stream, build_encoder_options(options))
  encoder.encode_stream(input_data)
end

#decompress(input_stream, output_stream, _options = nil) ⇒ void

This method returns an undefined value.

Decompress Deflate-compressed data

Parameters:

  • input_stream (IO)

    Input stream of compressed data

  • output_stream (IO)

    Output stream for decompressed data

  • options (Models::CompressionOptions)

    Decompression options



79
80
81
82
83
84
85
86
# File 'lib/omnizip/algorithms/deflate.rb', line 79

def decompress(input_stream, output_stream, _options = nil)
  if output_stream.respond_to?(:set_encoding)
    output_stream.set_encoding(Encoding::BINARY)
  end
  decoder = Decoder.new(input_stream)
  decompressed = decoder.decode_stream
  output_stream.write(decompressed)
end