Skip to content
C project #21Advanced

Huffman File Compressor

A real file compression utility: frequency analysis, a min-heap built Huffman tree, bit-level I/O and a self-describing file format.

Estimated time
~12 hours to build
Files
8 files · 542 lines
Technologies
C11, <stdint.h>, Binary file I/O

Introduction

Huffman coding is the entropy-coding stage of ZIP, PNG and JPEG. This project implements a complete compressor and decompressor that work on any file, with a compact header that lets the decoder rebuild the exact same tree.

Problem statement

Write a command-line tool that compresses a file into the .huf format and restores it exactly, prints the code table, and has a demo mode that compresses, decompresses and verifies a round trip. The format must be portable (explicit little-endian integers) and handle edge cases such as empty files and files with a single distinct byte.

Objectives

  • Build a Huffman tree with a binary min-heap
  • Write and read individual bits
  • Design a portable binary file format
  • Handle edge cases and errors precisely

Features

  • compress / decompress / codes / demo modes
  • Deterministic trees (tie-breaking)
  • Header stores only symbols that occur
  • Single-symbol and empty file support
  • Round-trip verification

Concepts demonstrated

  • Greedy algorithms
  • Priority queues
  • Bit manipulation
  • Endianness
  • File formats

System architecture

Pass 1 counts byte frequencies. build_tree pushes one leaf per symbol into a min-heap and repeatedly merges the two lightest nodes. assign_codes walks the tree to build code strings. Pass 2 writes the header (magic, size, symbol table) and packs code bits MSB-first into bytes. Decompression reads the header, rebuilds the identical tree and walks it bit by bit.

TextArchitecture diagram
input ─> count freq ─> min-heap ─> Huffman tree ─> codes ─> header + bitstream ─> .huf
.huf ─> header ─> same tree ─> walk bits ─> original bytes

Folder structure

TextProject tree
huffman-compressor/
├── README.md
├── Makefile
├── sample.txt
├── src/huffman.c
├── src/huffman.h
├── src/main.c
├── tests/args.txt
└── tests/expected-output.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/huffman.h
The file format description, stats struct and API.
src/huffman.c
Min-heap, tree building, code assignment, little-endian helpers, compress, decompress and the code table.
src/main.c
Command-line modes and round-trip verification.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app demo sample.txt # Linux / macOS
.\app.exe demo sample.txt # Windows

Test against the expected output

Terminal
./app demo sample.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# .\app.exe demo sample.txt > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
Most frequent symbols in sample.txt:
symbol freq code
' ' 203 110
e 135 010
t 98 1110
a 82 1010
r 70 0111
s 63 0011
n 60 0010
i 58 0000
compressed : 1234 -> 980 bytes (79.4%)
decompressed: 1234 bytes
round trip OK - files are identical

Testing

  • ./app demo sample.txt must match tests/expected-output.txt and report "round trip OK".
  • Compress an image or a PDF and decompress it; compare with cmp (Linux/macOS) or fc /b (Windows).
  • Compress an empty file and a file containing only "aaaa".

Common errors

Decompressed file has extra bytes

Stop decoding after original_bytes symbols; the last byte contains padding bits.

Files compressed on one machine fail on another

Write integers byte by byte in a fixed order (as write_u32/write_u64 do), never with fwrite of an int.

Possible improvements

  • Store the tree shape instead of frequencies for a smaller header
  • Canonical Huffman codes
  • Stream large files in blocks

Additional challenges

  1. 1Add LZ77 before Huffman coding to build a mini-DEFLATE.
  2. 2Measure compression ratios on text, source code and images, and explain the differences.
  3. 3Add a checksum (CRC-32) to detect corrupted files.

Built and tested it yourself? Mark it to update your dashboard.