# gregsh/parallel-zip architecture

> Architecture diagram of the GitHub repository gregsh/parallel-zip, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gregsh/parallel-zip
- Repository: https://github.com/gregsh/parallel-zip

## Overview

##### Project type and purpose
- This is a CLI tool (open-source utility) that runs on the JVM. It provides a “parallel zip” implementation in both Java and Kotlin, leveraging multiple cores to compress input files concurrently into a single ZIP archive far faster than the standard sequential approach. It also includes a merging capability (recombine existing ZIPs without recompression) and supports both a fat-jar and a GraalVM native image distribution.

##### File structure
- Top-level build files and config
- build.gradle.kts, gradlew, gradlew.bat, gradle/wrapper/…
- LICENSE.md, README.md, .gitignore
- Source folders
- src/main/java/parallelZip
– MainJava.java – Java entry point for parallel mode
– Sequential.java – Java entry point for the sequential comparison
- src/main/kotlin/parallelZip
– MainKotlin.kt – Kotlin entry point for parallel mode
– SingleEntryZipOutputStream.kt – helper to write one entry without a central directory
– ZipInputStreamEx.kt – extension to merge existing zips without recompression
- Resources
- META-INF/native-image/reflect-config.json – GraalVM reflection config

##### Key README insights
- Algorithm
1. Parallel phase: for each file, spin up a worker that
a. Wraps a ByteArrayOutputStream with SingleEntryZipOutputStream
b. Streams file bytes into that stream (skips central directory)
c. Stores (ZipEntry, compressed bytes) in a concurrent map
2. Sequential phase: single thread writes all pre-compressed entries’ raw bytes into a real ZipOutputStream, then closes to emit the central directory
- Uses Java Reflection to inject entries and offsets into java.util.zip.ZipOutputStream internals
- Performance numbers: ~8× speedup on multi-core
- Supports fat-jar and GraalVM native-image packaging

a. Main components
- CLI Launcher
– MainJava/MainKotlin
– Parses CLI args (output ZIP path + input files/dirs)
- File Enumerator
– Recursively lists all inputs
- Parallel Compressor Pool
– Thread-pool or parallel stream workers
– SingleEntryZipOutputStream + ByteArrayOutputStream per file
– Outputs (ZipEntry metadata + compressed byte array)
- In-Memory Buffer
– ConcurrentHashMap<ZipEntry, byte[]>
– Backpressure mechanism (e.g., blocking queue) if implemented
- Sequential Writer
– ZipOutputStream wrapping FileOutputStream
– Reflective injection of XEntry and offset
– Writes raw bytes from buffer, closes stream to flush central directory
- Merge Module (optional)
– ZipInputStreamEx reads existing zips, extracts entry headers + raw data pointers
- Packaging/Build
– Gradle → fat-jar or GraalVM native image

b. Relationships and interactions
1. CLI Launcher invokes File Enumerator
2. File list fed into Parallel Compressor Pool
3. Each compressor worker writes into In-Memory Buffer
4. Once compression phase completes (or incrementally), Sequential Writer drains the buffer in entry order
5. Final ZIP output on disk
6. (Optional) Merge Module can feed data directly into Sequential Writer without recompressing

c. Architectural patterns and design principles
- Producer-Consumer (parallel compression producers, sequential writer consumer)
- Single Responsibility (separate classes for entry streaming, CLI parsing, merge logic)
- Reflection as an Extension Point (to work around missing ZIP API)
- Immutable data handoff (ByteArrayOutputStream content + ZipEntry metadata)

d. Key technologies/frameworks
- JVM languages: Java 17+ and Kotlin
- java.util.zip APIs
- Kotlin Coroutines or Java parallel streams / ExecutorService
- Gradle with Kotlin DSL
- GraalVM native-image for AOT compilation

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI & Orchestration Layer
    subgraph "CLI & Orchestration"
        direction TB
        MainJava["MainJava (CLI Launcher)"]:::cli
        MainKotlin["MainKotlin (CLI Launcher)"]:::cli
        FileEnum["File Enumerator"]:::cli
    end

    %% Parallel Compression Layer
    subgraph "Parallel Compressor Pool"
        direction TB
        CompressorPool["Parallel Compressor Pool\n(Thread Pool)"]:::parallel
        SingleEntry["SingleEntryZipOutputStream"]:::parallel
    end

    %% In-Memory Buffer
    ConcurrentBuffer(("ConcurrentHashMap\n<ZipEntry, byte[]>")):::parallel

    %% Sequential I/O Layer
    subgraph "Sequential Writer"
        direction TB
        SeqWriter["Sequential Writer\n(ZipOutputStream + Reflection)"]:::sequential
    end

    %% Merge Module
    MergeModule["ZipInputStreamEx (Merge Module)"]:::sequential

    %% Output
    OutputFS["Output ZIP File"]:::sequential

    %% Build & Packaging Layer
    subgraph "Build & Packaging"
        direction TB
        Gradle["build.gradle.kts\n+ gradlew, gradlew.bat\n+ gradle/wrapper/..."]:::build
        ReflectConfig["GraalVM Reflect Config\nreflect-config.json"]:::build
        Docs["README.md, LICENSE.md, .gitignore"]:::build
    end

    %% Relationships
    MainJava -->|"args"| FileEnum
    MainKotlin -->|"args"| FileEnum
    FileEnum -->|"List<File>"| CompressorPool
    CompressorPool -->|"(ZipEntry, byte[])"| ConcurrentBuffer
    SingleEntry -->|"used by"| CompressorPool
    ConcurrentBuffer -->|"raw bytes"| SeqWriter
    SeqWriter -->|"writes to"| OutputFS
    MergeModule -.->|"zip entries"| SeqWriter

    %% Build outputs
    Gradle -->|"packages"| OutputFS
    ReflectConfig -->|"for native image"| Gradle
    Docs -->|"project info"| Gradle

    %% Click Events
    click MainJava "https://github.com/gregsh/parallel-zip/blob/main/src/main/java/parallelZip/MainJava.java"
    click MainKotlin "https://github.com/gregsh/parallel-zip/blob/main/src/main/kotlin/parallelZip/MainKotlin.kt"
    click SeqWriter "https://github.com/gregsh/parallel-zip/blob/main/src/main/java/parallelZip/Sequential.java"
    click SingleEntry "https://github.com/gregsh/parallel-zip/blob/main/src/main/kotlin/parallelZip/SingleEntryZipOutputStream.kt"
    click MergeModule "https://github.com/gregsh/parallel-zip/blob/main/src/main/kotlin/parallelZip/ZipInputStreamEx.kt"
    click ReflectConfig "https://github.com/gregsh/parallel-zip/blob/main/src/main/resources/META-INF/native-image/reflect-config.json"
    click Gradle "https://github.com/gregsh/parallel-zip/blob/main/build.gradle.kts"
    click Docs "https://github.com/gregsh/parallel-zip/blob/main/README.md"

    %% Styles
    classDef cli fill:#e0ffe0,stroke:#333,stroke-width:1px
    classDef parallel fill:#e0f0ff,stroke:#333,stroke-width:1px
    classDef sequential fill:#ffe0b3,stroke:#333,stroke-width:1px
    classDef build fill:#e0e0e0,stroke:#333,stroke-width:1px
```
