Skip to content

Vallabh-1504/Huffman-file-compressor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Huffman File Compressor

A file compression and decompression tool built using the Huffman Encoding Algorithm.
This project demonstrates lossless data compression by encoding frequently occurring characters with shorter binary codes.


Features

  • Compress any binary/text file using Huffman Greedy Encoding Algorithm.
  • Serialize and store the Huffman tree within the compressed file.
  • Decompress back to the original file without loss.
  • Reports file sizes and compression ratio.
  • CLI-based usage: simple and effective.
  • Supports extended characters and binary files.
  • optional ouput_file parameter

File Structure

├── Huffman.h # Header file for Huffman class ├── Huffman.cpp # implementation file for the Huffman class
├── main.cpp # main file for handling CLI ├── input.txt # Example input file ├── skeweddata.txt # file for compression demonstration └── README.md # Project documentation


How It Works

Compression:

  • Reads the input file and builds a frequency map.
  • Constructs the Huffman Tree.
  • Generates binary codes for characters.
  • Serializes the tree using pre-order traversal.
  • Encodes the content and writes binary data.

Decompression:

  • Deserializes the Huffman Tree, reads delimier and padding info.
  • Reads and decodes the binary data.
  • Restores the original content by traversing the tree.

Usage

1. Compile

g++ main.cpp Huffman.cpp -o huffman

2. Compress

./huffman compress input.txt compressed.bin

Or let program create a default output name (input.huf)

./huffman compress input.txt

3. Decompress

./huffman decompress compressed.bin output.txt

Sample Output

./huffman compress input.txt compressed.bin Compressed data written to: compressed.bin
original file size: 220.466KB
compressed file size :118.652KB
Compression ratio: 53.8189%

Decompression successful!
Restored file written to: output.txt
Restored file size: 220.466 KB

About

C++ Huffman coding file compressor and decompressor

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages