Skip to main content
Version: 2.x

Gzip

The gzip module compresses and decompresses the Gzip format. It is built on the ZIO Streams compression support, which uses the JVM standard library.

Add the module to your build.sbt:

libraryDependencies += "dev.zio" %% "zio-streams-compress-gzip" % "3.0.1"

GzipCompressorโ€‹

GzipCompressor is a Compressor that writes the Gzip format. Create one with GzipCompressor.apply to set its options, or use GzipCompressor.compress for the defaults:

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

val gzipped: ZStream[Any, Nothing, Byte] =
ZStream.fromIterable("Hello, ZIO!".getBytes("UTF-8")).via(GzipCompressor.compress)

To choose the compression level and strategy:

import zio.compress._

val fastest = GzipCompressor(
deflateLevel = DeflateCompressionLevel(1),
deflateStrategy = Some(DeflateStrategy.HuffmanOnly),
)

DeflateCompressionLevel(1) returns an Option, which matches the parameter type directly. When the value is invalid, pass None instead of a level.

GzipDecompressorโ€‹

GzipDecompressor is a Decompressor that reads the Gzip format:

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

val unzipped: ZStream[Any, Throwable, Byte] =
ZStream.fromFileName("file.gz").via(GzipDecompressor.decompress)

The decompressor accepts several concatenated Gzip members in one input and emits their content in order. This is the form that results from appending the output of several compressions to one file.

Optionsโ€‹

OptionTypeDefault
deflateLevelOption[DeflateCompressionLevel]None (ZIO's default, level 6)
deflateStrategyOption[DeflateStrategy]None (default strategy)
bufferSizeInt (compressor and decompressor)64 KiB

DeflateCompressionLevel ranges from 0 (no compression) to 9 (best compression). See Core Interfaces for the named constants.

DeflateStrategy has two values:

  • DeflateStrategy.Filtered suits data that a predictor filter has already transformed into many small values. It matches the zlib Z_FILTERED strategy.
  • DeflateStrategy.HuffmanOnly skips string matching and only applies Huffman coding. It is fast, but compresses less.

The bufferSize is the largest chunk emitted by the output stream.

Notesโ€‹

  • The compressor's error type is Nothing, so it cannot fail. Errors come from the upstream stream, not from compression.
  • The decompressor fails with a Throwable when the input is not valid Gzip.
  • The Gzip format is a single-file format. To compress several files into one archive, use a tar archive with Gzip around it. See Tar.

ZIO Chat

๐Ÿ”ฅ Ignite your ZIO journey with ZIO Chat! ๐Ÿ”ฅ Ready to unravel the intricacies of ZIO? As your trusty sidekick, I'm here to light the way. No matter if you're wandering through functional forests or traversing effectful landscapes, I've got answers to keep you on track. Your ZIO adventure starts here โ€“ ask me anything and let's explore together!