A command-line string compression tool implementing three classic compression algorithms in C++17
# 1. Compile
g++ src/*.cpp -Iinclude -std=c++17 -O2 -o bin/compressor
# 2. Run (compress)
bin/compressor -a rle -m c -i tests/puzzle.txt -o output.rle
# 3. Verify (decompress)
bin/compressor -a rle -m d -i output.rle -o decoded.txt| Algorithm | Flag | Description |
|---|---|---|
| Run-Length Encoding | rle |
Compresses consecutive repeated bytes |
| Huffman Coding | huff |
Variable-length prefix coding via binary tree (canonical) |
| Lempel-Ziv-Welch | lzw |
Dictionary-based adaptive compression (fixed 16-bit codes) |
# Direct compile
g++ src/*.cpp -Iinclude -std=c++17 -O2 -o bin/compressor
# Or using Makefile
makeClean build artifacts:
make cleanbin/compressor -a [rle|huff|lzw] -m [c|d] -i <input> -o <output>| Flag | Description |
|---|---|
-a |
Algorithm: rle, huff, or lzw |
-m |
Mode: c (compress) or d (decompress) |
-i |
Input file path |
-o |
Output file path |
Test file (tests/puzzle.txt): AAAAABBBBBCCCCCDDDDD
# RLE
bin/compressor -a rle -m c -i tests/puzzle.txt -o output.rle
bin/compressor -a rle -m d -i output.rle -o decoded.txt
# Huffman
bin/compressor -a huff -m c -i tests/puzzle.txt -o output.huff
bin/compressor -a huff -m d -i output.huff -o decoded.txt
# LZW
bin/compressor -a lzw -m c -i tests/puzzle.txt -o output.lzw
bin/compressor -a lzw -m d -i output.lzw -o decoded.txtWith any text file:
bin/compressor -a huff -m c -i myfile.txt -o myfile.huff
bin/compressor -a huff -m d -i myfile.huff -o myfile_restored.txtAfter compression, the tool prints:
Compression complete.
--------------------------------
Algorithm: huff
Execution Time: 0.12 ms
Original Size: 20 bytes
Compressed Size: 15 bytes
Compression Ratio: 1.33
Space Savings: 25.00%
- Compression Ratio = Original Size / Compressed Size
- Space Savings = (1 - Compressed Size / Original Size) × 100%
- C++17 compiler (g++ 7+)
- No external libraries — C++ Standard Library only