This version is still in development and is not considered stable yet. For the latest stable version, please use Compress4J 5.x!

Snappy

Snappy needs no extra dependency. It has two formats, so it has two Compression factories: Compression.snappyFramed() (.sz, for files and streams) and Compression.snappyRaw(), a single block.

Decompressor detects the framed format, never the raw format. Raw data passes through Decompressor.builder(p) unchanged: use Decompressor.builder(p, Compression.snappyRaw()). The raw format stores the uncompressed length first, so writing needs Compression.snappyRaw().uncompressedSize(length); a negative length throws IllegalArgumentException.

Decompressor enforces the default limits; a stream that expands past a ratio of 100 after the first MiB throws LimitExceededException.

Example Usage

Framed Snappy Compression
try (Compressor compressor = Compressor.builder(Path.of("example.sz"), Compression.snappyFramed())
        .build()) {
    compressor.write(Path.of("path/to/file.txt"));
}
Framed Snappy Decompression
try (Decompressor decompressor =
        Decompressor.builder(Path.of("example.sz")).build()) {
    decompressor.write(Path.of("path/to/file.txt"));
}
Raw Snappy Compression
Path source = Path.of("path/to/file.txt");
Compression snappy = Compression.snappyRaw().uncompressedSize(Files.size(source));
try (Compressor compressor =
        Compressor.builder(Path.of("example.snappy"), snappy).build()) {
    compressor.write(source);
}
Raw Snappy Decompression
try (Decompressor decompressor = Decompressor.builder(Path.of("example.snappy"), Compression.snappyRaw())
        .build()) {
    decompressor.write(Path.of("path/to/file.txt"));
}