Skip to main content

LZ77 vs DEFLATE

Project description

Compression algorithm comparisons

This project implements two compression algorithms:

  1. LZ77
  2. A simplified DEFLATE-style compressor

The simplified DEFLATE-style compressor in this repository is DEFLATE-inspired, not RFC-compliant DEFLATE. It uses:

  • LZ77 tokenization with literals and (distance, length) matches
  • Canonical Huffman coding over the token stream
  • Exact length and distance symbols instead of DEFLATE's block format, extra bits, and RFC headers

The code operates on raw file bytes. That means Unicode .txt files round-trip correctly as long as the original byte encoding is preserved, which the compressors do.

Algorithms

LZ77

The LZ77 compressor scans through the input with:

  • a configurable sliding window over previously seen bytes
  • a configurable lookahead buffer
  • greedy longest-match search

If it finds a match of at least min_match_length, it emits a back-reference token:

  • MatchToken(distance, length)

Otherwise it emits:

  • LiteralToken(byte_value)

Simplified DEFLATE-style

This compressor reuses the LZ77 tokenization stage, then Huffman-encodes the token stream:

  • literal bytes are encoded as symbols 0..255
  • an end-of-stream marker uses symbol 256
  • match lengths use symbols 257+
  • match distances are encoded with a separate Huffman alphabet

This keeps the design simple while still demonstrating why adding entropy coding on top of LZ77 often improves compression.

File Formats

.lz77

Simple custom binary format:

  • magic: LZ77
  • version byte
  • integers-encoded metadata:
    • window_size
    • lookahead_size
    • min_match_length
    • original_size
    • token_count
  • token stream:
    • literal token: tag 0x00, followed by 1 byte
    • match token: tag 0x01, followed by integers distance and integers length

.sdeflate

Simple custom DEFLATE-inspired format:

  • magic: SDFL
  • version byte
  • integers-encoded metadata:
    • window_size
    • lookahead_size
    • min_match_length
    • original_size
    • literal/length alphabet size
    • literal/length code lengths
    • distance alphabet size
    • distance code lengths
  • Huffman-coded bitstream

This format stores canonical Huffman code lengths so the decoder can rebuild the exact codebooks.

Project Layout

compressor/
  __init__.py
  __main__.py
  benchmark.py
  bits.py
  cli.py
  huffman.py
  lz77.py
  models.py
  sdeflate.py
  integers.py
tests/
  test_cli.py
  test_roundtrip.py

Running

You can run the project directly from the repository root:

python -m compressor --help

Compress with LZ77

python -m compressor compress lz77 path/to/file.txt
python -m compressor compress lz77 path/to/file.txt --window-size 8192 --lookahead-size 258 --min-match-length 3
python -m compressor compress lz77 path/to/file.txt --output path/to/file.txt.lz77

Decompress with LZ77

python -m compressor decompress lz77 path/to/file.txt.lz77
python -m compressor decompress lz77 path/to/file.txt.lz77 --output restored.txt

Compress with Simplified DEFLATE-style

python -m compressor compress sdeflate path/to/file.txt
python -m compressor compress sdeflate path/to/file.txt --window-size 8192 --lookahead-size 258 --min-match-length 3

Decompress with Simplified DEFLATE-style

python -m compressor decompress sdeflate path/to/file.txt.sdeflate

Benchmark One File

python -m compressor benchmark path/to/file.txt
python -m compressor benchmark path/to/file.txt --repeat 5

Benchmark All .txt Files in a Directory

python -m compressor benchmark path/to/texts
python -m compressor benchmark path/to/texts --csv benchmark_results.csv

Directory benchmarking scans recursively for *.txt files.

Benchmark Output

The benchmark prints a readable terminal table with:

  • file path
  • algorithm
  • original size
  • compressed size
  • compression ratio
  • compression time
  • decompression time
  • correctness check

Optional CSV export is supported with --csv.

Tests

Run the included verification suite:

python -m unittest discover -s tests -v

The tests cover:

  • round-trip correctness for both algorithms
  • empty input
  • highly repetitive input
  • mostly unique input
  • Unicode text
  • CLI smoke tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rahmatov_compressor-0.1.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rahmatov_compressor-0.1.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file rahmatov_compressor-0.1.0.tar.gz.

File metadata

  • Download URL: rahmatov_compressor-0.1.0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rahmatov_compressor-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ffa9d523c3389ff5473ee2344a95e6055400024fb50146c6d8675ca310856694
MD5 ab6e1b43ec952fc7f6be90285abac70c
BLAKE2b-256 22130f7d470dfaf8ba5372f051070bfc2f55f01b8686d90bd0940522ce4a0704

See more details on using hashes here.

Provenance

The following attestation bundles were made for rahmatov_compressor-0.1.0.tar.gz:

Publisher: publish.yml on rahmatov-abdullaxon/compressor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rahmatov_compressor-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for rahmatov_compressor-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 554275213f68a7a7e8069a050cc3d70e83cf6b91e335d0011d1f265ab6d7e87d
MD5 af40ad48c6b93b1572419249b733c0d6
BLAKE2b-256 2aef59581abf1cb159bfbd6d061d12db19cdba2fbf777e1f62a55f6f1c718c7b

See more details on using hashes here.

Provenance

The following attestation bundles were made for rahmatov_compressor-0.1.0-py3-none-any.whl:

Publisher: publish.yml on rahmatov-abdullaxon/compressor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page