Class: Omnizip::Algorithms::Deflate
- Inherits:
-
Omnizip::Algorithm
- Object
- Omnizip::Algorithm
- Omnizip::Algorithms::Deflate
- 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:
- LZ77 compression - Identifies repeated byte sequences
- 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
Class Method Summary collapse
-
.metadata ⇒ AlgorithmMetadata
Get algorithm metadata.
Instance Method Summary collapse
-
#compress(input_stream, output_stream, options = nil) ⇒ void
Compress data using Deflate algorithm.
-
#decompress(input_stream, output_stream, _options = nil) ⇒ void
Decompress Deflate-compressed data.
Methods inherited from Omnizip::Algorithm
inherited, #initialize, #with_filter
Constructor Details
This class inherits a constructor from Omnizip::Algorithm
Class Method Details
.metadata ⇒ AlgorithmMetadata
Get algorithm metadata
52 53 54 55 56 57 58 59 |
# File 'lib/omnizip/algorithms/deflate.rb', line 52 def self. Models::AlgorithmMetadata.new.tap do || .name = "deflate" .description = "Deflate compression using LZ77 and " \ "Huffman coding (RFC 1951)" .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
67 68 69 70 71 |
# File 'lib/omnizip/algorithms/deflate.rb', line 67 def compress(input_stream, output_stream, = nil) input_data = input_stream.read encoder = Encoder.new(output_stream, ()) encoder.encode_stream(input_data) end |
#decompress(input_stream, output_stream, _options = nil) ⇒ void
This method returns an undefined value.
Decompress Deflate-compressed data
79 80 81 82 83 84 85 86 |
# File 'lib/omnizip/algorithms/deflate.rb', line 79 def decompress(input_stream, output_stream, = 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 |