Skip to main content
Version: 2.x

LZ4

The lz4 module compresses and decompresses the LZ4 frame format. It is built on the official LZ4 Java library.

Add the module to your build.sbt:

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

Lz4Compressorโ€‹

Lz4Compressor is a Compressor that writes the LZ4 frame format. Use Lz4Compressor.apply to choose the block size, or Lz4Compressor.compress for the default block size:

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

val compressed: ZStream[Any, Throwable, Byte] =
ZStream.fromIterable("Hello, ZIO!".getBytes("UTF-8")).via(Lz4Compressor.compress)

To write blocks of 4 MiB:

import zio.compress._

val largeBlocks = Lz4Compressor(Lz4CompressorBlockSize.BlockSize4MiB)

Lz4Decompressorโ€‹

Lz4Decompressor is a Decompressor that reads the LZ4 frame format:

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

val decompressed: ZStream[Any, Throwable, Byte] =
ZStream.fromFileName("file.lz4").via(Lz4Decompressor.decompress)

Optionsโ€‹

OptionTypeDefault
blockSizeLz4CompressorBlockSize256 KiB
chunkSizeInt (decompressor only)4 KiB (ZStream.DefaultChunkSize)

Lz4CompressorBlockSize has four values: BlockSize64KiB, BlockSize256KiB, BlockSize1MiB, and BlockSize4MiB. A larger block size compresses better and uses more memory. To convert the numeric indicator of the LZ4 format (4 to 7), use Lz4CompressorBlockSize.fromLz4BlockSizeIndicator. It returns an Option, because other values are not valid.

Notesโ€‹

  • Both pipelines fail with a Throwable when the input is not a valid LZ4 frame.
  • The LZ4 frame format has no entry names or metadata. To compress several files, use a tar archive inside the LZ4 stream.

ZIO Chat

๐ŸŒŸ Greetings from ZIO Chat! ๐ŸŒŸ Have questions about ZIO? Look no further! I'm here to be your ZIO guru. Whether you're a seasoned pro or just starting out, I've got answers tailored just for you. Fire away with your ZIO queries, and I'll hit you back with the best insights I've got. Let's dive into the world of ZIO together!