Skip to content

[#64] Add archive repack command with configurable LZ4 block layout - #157

Closed
Mohith26 wants to merge 1 commit into
Unity-Technologies:mainfrom
Mohith26:feature/archive-repack
Closed

Mohith26 wants to merge 1 commit into
Unity-Technologies:mainfrom
Mohith26:feature/archive-repack

Conversation

@Mohith26

@Mohith26 Mohith26 commented Nov 4, 2026

Copy link
Copy Markdown

Closes #64.

Adds archive repack, which rewrites an existing Unity Archive into a new one with the same files and a block layout you choose. It covers the parameters listed in the issue:

  • --block-size: max uncompressed bytes per block (default 128 KB, like Unity)
  • --max-compressed-block-size: cap on the stored size of a block; a block that would compress to more is shortened (binary search on the prefix length)
  • --split-at-file-boundaries: start a new block for each file instead of spanning
  • --file-alignment: padding between files in the uncompressed data
  • --min-compression-ratio: store a block uncompressed unless it compresses by at least this ratio
  • -c None|Lz4|Lz4HC, and --block-alignment for format v9 archives (positions blocks on disk, like the 6.7 writer does with 16 bytes)

The content is read through UnityFileSystemApi, so LZMA inputs work too. The output keeps the input's format version, Unity version and node flags, and the metadata is written LZ4HC right after the header. The header/metadata serialization lives in a new UnityBinaryFormat/ArchiveWriter.cs, the mirror of ArchiveDetector.

By default the output is verified: both archives are read back through UnityFileSystemApi and every file is compared by SHA-256; on a mismatch the output is deleted. One thing I ran into: with both archives mounted at the same time (different mount points, same file names), reads of the repacked archive returned the original's bytes, so a tampered file passed. The check now mounts them one after the other, and there is a test that flips a byte and expects the failure.

The command prints a before/after report (size, block count, blocks stored raw, largest block, and a "read amplification" figure: uncompressed bytes of the blocks you have to touch to load each file on its own, divided by total file size). Some numbers on the repo's test data:

LeadingEdgeBuilds/AssetBundlesLz4/scenes (v9, Unity 6.7, 362,352 bytes, read amp 1.00x):

Layout Size vs original Blocks Read amp
default (LZ4HC, 128 KB, span) 361,976 -0.1% 7 1.49x
512 KB blocks 354,175 -2.3% 2 2.03x
32 KB blocks 371,902 +2.6% 25 1.12x
128 KB, split at files 364,873 +0.7% 9 1.00x
LZ4 (fast) 128 KB 403,966 +11.5% 7 1.49x

AssetBundles/2023.1.0a16/assetbundle (LZMA, 2,211,569 bytes, 3,781,435 uncompressed): LZ4HC 128 KB gives 3,015,300 bytes; with LZ4 (fast), 20 of 29 blocks fall back to uncompressed because the texture data does not compress.

These are deterministic sizes from the CLI (-f Json), not timings. All outputs above verified.

Testing: dotnet build -c Release (0 warnings) and dotnet test on Apple Silicon arm64 (macOS): Analyzer.Tests 67, UnityFileSystem.Tests 442 (+10 skipped), UnityDataTool.Tests 380 including 23 new tests in ArchiveRepackTests.cs. The tests cover each option on v8 and v9 inputs (I also repacked the 2019.4 v6 bundle by hand), check the layout through ArchiveDetector, compare content independently via archive extract, check that dump output is identical before and after, and cover the error cases. dotnet format whitespace --verify-no-changes is clean on the changed files.

I have not loaded a repacked bundle in a Player; the docs note to test on the target platform before shipping one.

This was written with AI assistance (Claude). I reviewed the code and ran the tests above locally.

Rewrites an existing Unity Archive into a new one with the same files and a
chosen block layout: block size, a cap on the compressed block size, splitting
at file boundaries, file alignment, block alignment (format v9) and a minimum
compression ratio below which blocks are stored uncompressed. The output keeps
the input's format and Unity version and is verified by reading both archives
back through UnityFileSystemApi.

AI-assisted (Claude); reviewed and tested locally with dotnet test.
@Mohith26 Mohith26 closed this by deleting the head repository Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support repacking Archives (Lz4 compression configuration)

1 participant