Self-made C++ file archiver and archive extractor programs based on Huffman's lossless compression algorithm.
The Huffman algorithm
is a lossless data compression technique that assigns variable-length codes to characters in a text based on their frequency of occurrence. It creates a binary tree with the most frequent characters at the top and the least frequent at the bottom, and assigns a 0
or 1
to each path from the root to a leaf node. This way, the characters that appear more frequently in the text will have shorter codes, resulting in a more efficient compression.
The Compressor
is a 2-pass program
that reads input files twice.
In the first pass, the program counts the frequency of occurrence of every unique character in the input text. It then uses this information to create a Huffman tree, where the most frequent characters have shorter codes. The program then writes the Huffman codes for each character to the compressed file, for decompression purposes.
In the second pass, the program translates the input text using the Huffman codes generated in the first pass, and writes the compressed text to a new file.
This is how the code i provided earlier works, it reads the text as input and generate frequency of each character and then it uses this frequency to generate huffman tree and then it uses this tree to generate codes for each character. It then uses these codes to compress the text, and write the compressed text to a new file.
The Decompressor
is a 1-pass program:
The Decompressor
first reads translation info and creates a binary tree from it. After this process is done, it uses this binary translation tree to decode the rest of the file
- Make sure you have Git and a C++ compiler installed on your computer.
Clone the repository that contains the Huffman compression code by running the following command in your terminal or command prompt:
git clone https://github.com/shreyanshmalvya/Huffman_Compressor.git
Navigate to the directory where the repository has been cloned:
cd Huffman_Compressor
Compile the code by running the following command:
g++ -o huffman huffman_compressor.cpp
Run the program by executing the following command:
./huffman_compressor
- The program will prompt you to enter a string of text to compress. Type or paste in the text you want to compress and press enter.
- The program will then prompt you to enter a file name to save the compressed text to. Enter a file name and press enter.
- The program will compress the text using the Huffman algorithm and save the compressed text to the specified file.
The program will display a message indicating that the compression is complete and the file has been saved.