A simple text file compression and decompression tool built using Huffman Coding, a lossless data compression algorithm. Credits: Coding Challenges
This project demonstrates:
- Building a Huffman Tree from character frequencies.
- Creating a prefix-free binary code table.
- Encoding input text into a compressed binary stream.
- Writing a header + compressed data to a file.
- Decoding back to the original text file.
- Frequency Analysis β Count frequency of each character/byte in the file.
- Build Huffman Tree β Use a priority queue to merge least frequent nodes.
- Generate Codes β Traverse the tree (DFS) to assign prefix-free codes.
- Encoding β Replace characters with codes, pack bits into bytes.
- Write Output β Save header (code map + padding info) + compressed data.
- Decoding β Read header, reconstruct tree/code map, decode compressed bits.
Prerequisites
- Java 8 or higher
- Git
The program uses a GUI to choose file for input:
- Clone the repo
- Navigate to file_compressor/src/main/java/com/compressor
- Execute HuffmanEncoder.java
- Select .txt file from GUI
- The compressed file(.bin) will be present in the parent path of the selected file
- For decoding, execute HuffmanDecoder.java
- Select .bin file from GUI
- The decompressed file(.txt) will be present in the parent path of the selected file
| Input Size | Output Size | Compression |
|---|---|---|
| 1.0 MB | 490 KB | ~51% |
| 3.7 MB | 2.2 MB | ~59% |
π Works best on text files with many repeating characters.
This project is licensed under the MIT License.