This project implements a File Compression and Decompression system using Huffman Encoding, a popular algorithm for lossless data compression. The algorithm is based on assigning shorter codes to more frequent characters and longer codes to less frequent characters, helping reduce the overall file size.
This project demonstrates the usage of Trees and Linked Lists in the implementation of Huffman Encoding and Decompression.
- Compression: Compresses a text file by encoding it with Huffman codes.
- Decompression: Decompresses the encoded file back to its original form using the Huffman tree.
- Text File Input and Output: Supports reading from a
.txtfile for compression and saving output to.txtfor decompression.
- Programming Language: C++
- IDE: Code::Blocks / Visual Studio Code / Turbo C++
- Data Structures:
- Binary Trees (Huffman Tree)
- Priority Queue (Min Heap using STL)
- Hash Map (for frequency and code mapping)
- File Formats:
.txt,.huff
- Frequency Calculation: Calculate the frequency of each character in the input file.
- Huffman Tree Construction: Build a priority queue and iteratively combine nodes to form the Huffman Tree.
- Code Generation: Traverse the Huffman Tree to generate binary codes for each character.
- Compression: Encode the input file using the generated Huffman codes and store metadata (tree structure and padding) for later use.
- Decompression: Read the metadata and binary data, rebuild the Huffman tree, and decode the binary data back into the original text.
- Input a
.txtfile (e.g.,input.txt). - The program calculates the frequency of each character.
- It then builds the Huffman Tree and generates the Huffman codes.
- The encoded data is saved in a
.hufffile along with metadata (tree structure and padding).
- Input a
.hufffile containing the encoded data. - The program extracts the Huffman Tree and binary data.
- The binary data is decoded using the Huffman Tree, and the original file is reconstructed and saved as
output.txt.
-
Compress a File:
- Run the program and choose option 1 (Compress File).
- Enter the name of the input file (e.g.,
input.txt). - The program will generate a compressed
.hufffile.
-
Decompress a File:
- Run the program and choose option 2 (Decompress File).
- Enter the name of the compressed file (e.g.,
compressed.huff). - The program will output the decompressed file as
output.txt.
Before Compression:
input.txt: A text file containing the text "This is an example of Huffman Encoding".
After Compression:
compressed.huff: A binary-encoded file with Huffman codes and tree metadata.
After Decompression:
output.txt: The original text file is restored: "This is an example of Huffman Encoding".
- Support for compressing image and audio files.
- Custom binary file formats for more efficient storage.
- GUI-based compression tool using frameworks like Qt or Python.
- Cormen, Leiserson, Rivest – Introduction to Algorithms.
- GeeksForGeeks – www.geeksforgeeks.org.
- TutorialsPoint – www.tutorialspoint.com.
- YouTube: SimpleSnippets, CodeWithHarry (for C++ DSA tutorials).
- Name: [Sharad Kalathiya].