LZ77 vs DEFLATE
Project description
Compression algorithm comparisons
This project implements two compression algorithms:
- LZ77
- 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_sizelookahead_sizemin_match_lengthoriginal_sizetoken_count
- token stream:
- literal token: tag
0x00, followed by 1 byte - match token: tag
0x01, followed by integersdistanceand integerslength
- literal token: tag
.sdeflate
Simple custom DEFLATE-inspired format:
- magic:
SDFL - version byte
- integers-encoded metadata:
window_sizelookahead_sizemin_match_lengthoriginal_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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ffa9d523c3389ff5473ee2344a95e6055400024fb50146c6d8675ca310856694
|
|
| MD5 |
ab6e1b43ec952fc7f6be90285abac70c
|
|
| BLAKE2b-256 |
22130f7d470dfaf8ba5372f051070bfc2f55f01b8686d90bd0940522ce4a0704
|
Provenance
The following attestation bundles were made for rahmatov_compressor-0.1.0.tar.gz:
Publisher:
publish.yml on rahmatov-abdullaxon/compressor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rahmatov_compressor-0.1.0.tar.gz -
Subject digest:
ffa9d523c3389ff5473ee2344a95e6055400024fb50146c6d8675ca310856694 - Sigstore transparency entry: 1228994378
- Sigstore integration time:
-
Permalink:
rahmatov-abdullaxon/compressor@8fc028add9db4e539f71329141a5cd7b6800d21e -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/rahmatov-abdullaxon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8fc028add9db4e539f71329141a5cd7b6800d21e -
Trigger Event:
release
-
Statement type:
File details
Details for the file rahmatov_compressor-0.1.0-py3-none-any.whl.
File metadata
- Download URL: rahmatov_compressor-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
554275213f68a7a7e8069a050cc3d70e83cf6b91e335d0011d1f265ab6d7e87d
|
|
| MD5 |
af40ad48c6b93b1572419249b733c0d6
|
|
| BLAKE2b-256 |
2aef59581abf1cb159bfbd6d061d12db19cdba2fbf777e1f62a55f6f1c718c7b
|
Provenance
The following attestation bundles were made for rahmatov_compressor-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on rahmatov-abdullaxon/compressor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rahmatov_compressor-0.1.0-py3-none-any.whl -
Subject digest:
554275213f68a7a7e8069a050cc3d70e83cf6b91e335d0011d1f265ab6d7e87d - Sigstore transparency entry: 1228994470
- Sigstore integration time:
-
Permalink:
rahmatov-abdullaxon/compressor@8fc028add9db4e539f71329141a5cd7b6800d21e -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/rahmatov-abdullaxon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8fc028add9db4e539f71329141a5cd7b6800d21e -
Trigger Event:
release
-
Statement type: