# yanikusgg/huffman-archiver architecture

> Architecture diagram of the GitHub repository yanikusgg/huffman-archiver, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yanikusgg/huffman-archiver
- Repository: https://github.com/yanikusgg/huffman-archiver

## Overview

##### Project type and purpose
- This is a command-line file archiver written in C++ that implements Huffman coding for lossless compression and decompression.
- It’s composed of a small core library (Huffman tree construction, bit‐level I/O, encoding/decoding) plus a CLI front end.

##### File structure
- CMakeLists.txt → build configuration (CMake).
- main.cpp → parses command-line arguments, drives compress/decompress logic.
- bitstream.cpp/.h → low-level bit reader/writer for packed output.
- coder.cpp/.h → high-level encode() and decode() interfaces.
- huffmantree.cpp/.h → frequency analysis and Huffman tree construction.
- huffman_constants.h → constants (e.g., special markers).

##### Extract insights from README
- CLI interface:
- `archiver -c archive_name file1 [file2 …]` → compress → write one archive file.
- `archiver -d archive_name` → decompress.
- `archiver -h` → help.
- All filenames and directory context preserved.

a. Main components:
- CLI Parser (main.cpp)
- Compression Engine (coder)
- Huffman Tree Builder (huffmantree)
- Bitstream I/O (bitstream)
- File I/O layer (filesystem reads/writes)
b. Relationships & data flow:
1. User enters CLI command → CLI Parser
2. Parser dispatches to Compression or Decompression use case in Coder
3a. Compress path:
- File I/O reads raw bytes → Huffman Tree Builder computes frequencies → builds tree → derives codebook
- Bitstream I/O writes header (tree structure + constants) → encodes file bytes via codebook → writes bitstream → closes archive
3b. Decompress path:
- Bitstream I/O reads archive header → reconstructs Huffman tree → reads bitstream → decodes bits → File I/O writes original files
c. Architectural patterns & design principles:
- Modular separation: CLI layer, core algorithm modules, I/O layer
- Pipeline pattern: sequential processing stages for compress/decompress
- Single Responsibility Principle: each module has one well-defined role
d. Key technologies & frameworks:
- C++17 (or later)
- CMake for build orchestration
- Standard filesystem library for file operations

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "CLI Layer"
        CLI["CLI Parser\n(main.cpp)"]:::cli
    end

    subgraph "Core Engine"
        Coder["Compression Engine\n(coder.cpp, coder.h)"]:::core
        HTB["Huffman Tree Builder\n(huffmantree.cpp, huffmantree.h)"]:::core
        Const["Huffman Constants\n(huffman_constants.h)"]:::core
    end

    subgraph "I/O Layer"
        FileRead["Raw Files"]:::artifact
        BitIO["Bitstream I/O\n(bitstream.cpp, bitstream.h)"]:::io
        FileWrite["Archive/Original Files"]:::artifact
    end

    CMake["Build Config\n(CMakeLists.txt)"]:::build
    README["Documentation\n(readme.md)"]:::build

    CLI -->|"compress/decompress"| Coder

    Coder -->|"raw bytes"| FileRead
    FileRead -->|"frequency analysis"| HTB
    HTB -->|"codebook"| Coder
    Coder -->|"write bitstream"| BitIO
    BitIO -->|"archive file"| FileWrite

    Coder -->|"read bitstream"| BitIO
    BitIO -->|"header/tree"| HTB
    HTB -->|"reconstructed tree"| Coder
    Coder -->|"decoded bytes"| FileWrite

    %% Click Events
    click CLI "https://github.com/yanikusgg/huffman-archiver/blob/main/main.cpp"
    click Coder "https://github.com/yanikusgg/huffman-archiver/blob/main/coder.cpp"
    click Coder "https://github.com/yanikusgg/huffman-archiver/blob/main/coder.h"
    click HTB "https://github.com/yanikusgg/huffman-archiver/blob/main/huffmantree.cpp"
    click HTB "https://github.com/yanikusgg/huffman-archiver/blob/main/huffmantree.h"
    click Const "https://github.com/yanikusgg/huffman-archiver/blob/main/huffman_constants.h"
    click BitIO "https://github.com/yanikusgg/huffman-archiver/blob/main/bitstream.cpp"
    click BitIO "https://github.com/yanikusgg/huffman-archiver/blob/main/bitstream.h"
    click CMake "https://github.com/yanikusgg/huffman-archiver/blob/main/CMakeLists.txt"
    click README "https://github.com/yanikusgg/huffman-archiver/blob/main/readme.md"

    classDef cli fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef core fill:#90EE90,stroke:#333,stroke-width:1px
    classDef io fill:#FFA500,stroke:#333,stroke-width:1px
    classDef build fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef artifact fill:#FFFACD,stroke:#333,stroke-width:1px,shape:cylinder
```
