Skip to content

Compression ​

Zstd and Gzip compress byte arrays and files with no extra dependency. Parquet, Arrow, and other formats use more codecs, listed below.

APIJVMNode.jsBrowserNativeImplementation
Zstd, byte arraysYesYesYesYesPure Scala
Zstd, filesYesYesNoYesPure Scala
Gzip, byte arrays and filesYesYesNoYesThe platform's zlib

Zstandard ​

Zstd compresses in pure Scala on every platform, including browsers (byte arrays only there).

scala
import wvlet.uni.io.Zstd

// In-memory compression, with an optional level from 1 (fastest) to 22 (default: 3)
val data = "hello world".getBytes("UTF-8")
val zstdData = Zstd.compress(data, level = 9)
val restored = Zstd.decompress(zstdData)

// Set the largest decompressed size to accept (default: 256 MiB)
val limited = Zstd.decompress(zstdData, maxSize = 64 * 1024 * 1024)

// Files readable and writable with the zstd command. Whole files are held in memory
Zstd.compressFile("data.json", "data.json.zst")
Zstd.decompressFile("data.json.zst", "data.json")

Levels 10 to 22 are accepted and compress as level 9. Decompression reads all frames of the input and verifies their checksums; compress writes no checksum. Data over maxSize fails with ZstdSizeLimitException. Malformed data and frames that need a dictionary fail with its parent ZstdException, an IOException.

Gzip ​

scala
import wvlet.uni.io.Gzip

// In-memory compression
val data = "hello world".getBytes("UTF-8")
val gzipped = Gzip.compress(data)
val original = Gzip.decompress(gzipped)

// Files: streamed on the JVM and Native, and read whole on Node.js
Gzip.compressFile("large.txt", "large.txt.gz")
Gzip.decompressFile("large.txt.gz", "large.txt")

Gzip.decompress has no size limit. Corrupt data fails with an IOException.

Codecs by format ​

These codecs are built into Uni. Readers detect the codec, and writers take it as an option.

CodecParquetArrow IPC (write)Iceberg Avro metadata (read)
ZstandardRead, writeYesYes
SnappyRead, write (default)NoYes
LZ4Read, write as LZ4_RAW; read the deprecated LZ4LZ4 framesNo
Gzip / DEFLATEGzip: read, write (writes not in browsers)NoDEFLATE
BrotliReadNoNo

Elsewhere:

See Parquet and Arrow IPC for their compression options.

Released under the Apache 2.0 License.