Skip to content

Latest commit

Β 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“¦ Huffman Compression in Java

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.

πŸ› οΈ How It Works

  1. Frequency Analysis β†’ Count frequency of each character/byte in the file.
  2. Build Huffman Tree β†’ Use a priority queue to merge least frequent nodes.
  3. Generate Codes β†’ Traverse the tree (DFS) to assign prefix-free codes.
  4. Encoding β†’ Replace characters with codes, pack bits into bytes.
  5. Write Output β†’ Save header (code map + padding info) + compressed data.
  6. Decoding β†’ Read header, reconstruct tree/code map, decode compressed bits.

βš™οΈ How to use

Prerequisites

  • Java 8 or higher
  • Git

The program uses a GUI to choose file for input:

  1. Clone the repo
  2. Navigate to file_compressor/src/main/java/com/compressor
  3. Execute HuffmanEncoder.java
  4. Select .txt file from GUI
  5. The compressed file(.bin) will be present in the parent path of the selected file
  6. For decoding, execute HuffmanDecoder.java
  7. Select .bin file from GUI
  8. The decompressed file(.txt) will be present in the parent path of the selected file

πŸ“Š Benchmarks

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.

πŸ“œ License

This project is licensed under the MIT License.

About

Simple text file compression and decompression tool

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages