Skip to content

Repository files navigation

📦 HuffmanZipper

A custom file compression tool built in C++ that implements the Huffman Coding algorithm. It reduces file sizes by analyzing character frequencies and generating variable-length binary codes, packing data down to the individual bit level.

🚀 Features

  • Bit-Level Manipulation: Implements a custom BitWriter and BitReader to handle raw binary I/O, allowing storage of non-byte-aligned codes (e.g., 3-bit sequences).
  • Custom File Format: Generates a binary file with a metadata header, enabling the tool to reconstruct the compression tree without external files.
  • Deterministic Compression: Uses std::map and tie-breaking logic to ensure the compression tree is identical every time, preventing data corruption across different systems.
  • End-to-End Pipeline: Handles the full lifecycle: Frequency Analysis -> Tree Construction -> Encoding -> Decoding.

🛠️ How It Works

  1. Frequency Analysis: Scans the input text to count how often each character appears.
  2. Tree Construction: Builds a Binary Tree (Priority Queue) where frequent characters (like 'e') are near the top and rare characters (like 'z') are at the bottom.
  3. Code Generation: Traverses the tree to assign binary codes. Going left adds a 0, going right adds a 1.
  4. Bit Packing: Buffers these codes into 8-bit chunks (bytes) and flushes them to a .bin file.
  5. Decompression: Reads the file header to rebuild the exact same tree, then follows the raw bits to trace paths back to the original characters.

💻 Usage

The current build runs a demonstration of the compression flow on a specific file.

  1. Prepare the Data: Ensure you have a folder named resources in your project directory containing a file named input.txt.

  2. Run the Program:

    ./HuffmanZipper
  3. Check Results: The program will generate:

    • resources/output.bin (The compressed binary file).
    • resources/restored.txt (The decompressed text, identical to input).

    It will also print statistics to the console:

    Original Bits for 'a': 8
    Compressed Bits for 'a': 3
    SUCCESS: The math checks out.
    

🔧 Build Instructions

  1. Prerequisites: Visual Studio 2022 (or any standard C++ compiler).
  2. Clone the repo:
    git clone https://github.com/Dronquavious/HuffmanZipper.git
  3. Open Project: Open HuffmanZipper.slnx in Visual Studio.
  4. Build: Press Ctrl + Shift + B or click Build Solution.

📂 Project Structure

  • src/: Main application source code.
  • resources/: Input text files and output binary data.

🧠 Concepts Applied

  • Data Structures: Priority Queues (Min-Heap), Binary Trees, Maps.
  • Systems Programming: Bitwise operators (<<, >>, &, |), Binary File Streams, Memory Management.

📄 License

This project is open source and under the MIT License. Feel free to use it for learning or as a base for your own project!

Contact

If you have questions, feedback, or want to discuss the project, you can reach me at:

Feel free to reach out — I’m happy to talk about the project or help you get started.

About

A C++ CLI tool for lossless file compression using the Huffman Coding algorithm and raw binary I/O.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages