Skip to main content
Version: 2.x

Compression and archives with zio-streams

ZIO Streams Compress integrates several compression algorithms and archive formats with ZIO Streams. Every codec and archive format is a pipeline, so it fits into a stream with via like any other operator.

Production Ready CI Badge Sonatype Releases Sonatype Snapshots javadoc ZIO Streams Compress docs Scala Steward badge

Installationโ€‹

To use the library, add one or more of the following lines to your build.sbt file:

libraryDependencies += "dev.zio" %% "zio-streams-compress-brotli" % "3.0.1"
libraryDependencies += "dev.zio" %% "zio-streams-compress-brotli4j" % "3.0.1"
libraryDependencies += "dev.zio" %% "zio-streams-compress-bzip2" % "3.0.1"
libraryDependencies += "dev.zio" %% "zio-streams-compress-gzip" % "3.0.1"
libraryDependencies += "dev.zio" %% "zio-streams-compress-lz4" % "3.0.1"
libraryDependencies += "dev.zio" %% "zio-streams-compress-snappy" % "3.0.1"
libraryDependencies += "dev.zio" %% "zio-streams-compress-tar" % "3.0.1"
libraryDependencies += "dev.zio" %% "zio-streams-compress-zip" % "3.0.1"
libraryDependencies += "dev.zio" %% "zio-streams-compress-zip4j" % "3.0.1"
libraryDependencies += "dev.zio" %% "zio-streams-compress-zstd" % "3.0.1"

Each module depends on core, which is pulled in automatically. Add only the modules you use.

As of 2.1.3, zio-streams-compress requires zio 2.1.25 or later.

The library is published for Scala 3, 2.13, and 2.12, and runs on the JVM. Scala.js and Scala Native are not supported. PRs for them are welcome.

Choosing a Codecโ€‹

The table lists each codec with its module. Compression and decompression are both available unless the table says otherwise.

FormatModuleCompressDecompressNotes
GzipgzipYesYesWidely supported. Built on the JDK.
ZstdzstdYesYesGood ratio at high speed. Dictionary on write only.
BrotlibrotliNoYesDecompression only, pure Java.
Brotlibrotli4jYesYesCompresses text well. Loads a native library.
Bzip2bzip2YesYesGood ratio. Uses a lot of memory.
LZ4lz4YesYesVery fast. Favors speed over ratio.
SnappysnappyYesYesFast. Three write formats.
ZIPzipYesYesArchive format. Entries may have unknown size.
ZIPzip4jYesYesArchive format. Reads password-protected archives.
TartarYesYesArchive format. No compression of its own.

For a bundle of files, pick an archive format (ZIP or Tar). For a single stream of bytes, pick a codec. A .tar.gz file is a Tar archive wrapped in Gzip.

Core Conceptsโ€‹

The library has two families of pipeline. A compressor turns bytes into compressed bytes, and a decompressor does the reverse. An archiver turns a stream of named entries into the bytes of an archive, and an unarchiver does the reverse. Every codec in the table is a compressor, a decompressor, or both. Every archive format is an archiver, an unarchiver, or both.

The Core Interfaces reference describes these types in full. The Tutorial builds a small program that uses them.

Exampleโ€‹

The example below compresses a file with Gzip, lists the entries of a Tar archive compressed with Gzip, and creates a ZIP archive:

import zio._
import zio.compress._
import zio.stream._

object ExampleApp extends ZIOAppDefault {
override def run: ZIO[Any, Any, Any] =
for {
// Compress a file with Gzip
_ <- ZStream
.fromFileName("file")
.via(GzipCompressor.compress)
.run(ZSink.fromFileName("file.gz"))

// List all items in a gzipped tar archive
_ <- ZStream
.fromFileName("file.tgz")
.via(GzipDecompressor.decompress)
.via(TarUnarchiver().list)
.mapZIO { entry =>
Console.printLine(s"${entry.name} ${entry.uncompressedSize.getOrElse(-1L)}")
}
.runDrain

// Create a ZIP archive with one entry
_ <- ZStream(entry("file1.txt", "Hello world!".getBytes("UTF-8")))
.via(ZipArchiver().archive)
.run(ZSink.fromFileName("file.zip"))
} yield ()

private def entry(
name: String,
content: Array[Byte],
): (ArchiveEntry[Option, Any], ZStream[Any, Throwable, Byte]) =
(ArchiveEntry[Option, Any](name), ZStream.fromIterable(content))
}

The Zip4J module also supports encryption and decryption of zip files. See the Zip4J reference.

Unarchivingโ€‹

As of zio-streams-compress 2.0, all unarchivers require the consumer to fully read the entry's contents before the next archive entry is emitted.

โš ๏ธ Not reading the entry's content causes the unarchive pipeline to lock up and halt your program.

In zio-streams-compress 1.x, consuming the next archive entry, even by accident for example by using buffering or rechunking, corrupts the content stream of all archive entries. With the new requirement the unarchiver can continue to operate in a streaming fashion without corrupting content.

Most of the time the new requirement should not be an issue. For example, you can concatenate unarchive and archive pipelines without problems.

Two cases need care:

  • You only need the metadata of the entries. Use the unarchiver's list method, which reads each entry's content behind the scenes.
  • You need buffering, aggregation, rechunking or parallel processing to improve throughput. Read each entry's content in the step that handles the entry, then do the buffering, aggregation or parallel work. The Read Archives Safely guide shows how, and how to limit the size of each entry.

Where to Go Nextโ€‹

ZIO Chat

Hello! ๐Ÿ˜Š I'm your ZIO Chat Bot, designed to be your go-to resource for all things ZIO! Whether you're new to this powerful functional effect system or a seasoned pro, I'm here to assist you. ZIO empowers you to build concurrent and resilient applications in Scala, offering referential transparency, composable data types, type-safety, and more. So, go ahead and ask me anything about ZIO โ€“ I'm here to provide the answers you need! ๐Ÿค–