ParallelZipWriter

public final class ParallelZipWriter

Writes a ZIP archive whose bytes are identical to what java.util.zip.ZipOutputStream would have produced for the same ordered entry list, but compresses the entries concurrently on a bounded pool instead of one at a time on the calling thread.

Deflating the merged resource pack is the single most expensive step in a mix cycle and it is embarrassingly parallel: ZipOutputStream calls Deflater.reset() between entries, so every entry's compressed bytes depend only on that entry's own content. This class exploits that — worker threads produce (crc, deflated bytes) pairs, and the calling thread splices them into the archive in the caller's supplied order.

Why byte-identity matters

RSPM hands clients a SHA-1 of the archive. ZipUtil sorts directory children and pins setTime(0L) on every entry precisely so an unchanged pack hashes to an unchanged value across restarts. Anything that perturbs the archive bytes forces every player on the server to redownload the pack. So this writer does not merely produce a deterministic archive — it reproduces ZipOutputStream's exact container layout:

  • local headers carry the data-descriptor flag (0x08) plus the UTF-8 name flag (0x800) and zeroed crc/size fields, because ZipOutputStream does not know an entry's size when it writes the header;
  • each entry's real crc/sizes follow the compressed bytes in a data descriptor;
  • the central directory and end-of-central-directory records match field for field, including the ZIP64 promotions ZipOutputStream applies past 4 GiB / 65,535 entries.

ParallelZipWriterDeterminismTest pins this down by zipping the same trees through a literal ZipOutputStream and through this class and asserting identical SHA-1.

Cancellation

CancellationException propagates from both the splice loop and the worker tasks, so a shutdown mid-archive aborts promptly rather than finishing a doomed 80 MB write. Workers poll the supplied BooleanSupplier between deflate chunks; the pool is shut down with shutdownNow() on the way out so in-flight compressions are interrupted.

Types

Link copied to clipboard
public final class Entry extends Record
One archive member.

Functions

Link copied to clipboard
public static void write(List<ParallelZipWriter.Entry> entries, OutputStream out, BooleanSupplier cancellationRequested)
Writes entries to out in the order given.