Skip to main content
Version: 2.x

Bzip2

The bzip2 module compresses and decompresses the Bzip2 format. It is built on the Apache Commons Compress library.

Add the module to your build.sbt:

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

Bzip2Compressorโ€‹

Bzip2Compressor is a Compressor that writes the Bzip2 format. Use Bzip2Compressor.apply to set the block size, or Bzip2Compressor.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(Bzip2Compressor.compress)

To write blocks of 100 KB instead of the default 900 KB:

import zio.compress._

val smallBlocks = Bzip2Compressor(blockSize = Bzip2BlockSize(1))

Bzip2BlockSize(1) returns an Option, which matches the blockSize parameter type.

Bzip2 uses a lot of memory. The memory needed grows with the block size. See the Apache Commons Compress documentation for the heap needed for each block size.

Bzip2Decompressorโ€‹

Bzip2Decompressor is a Decompressor that reads the Bzip2 format:

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

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

Optionsโ€‹

OptionTypeDefault
blockSizeOption[Bzip2BlockSize]None (900 KB)
chunkSizeInt (decompressor only)4 KiB (ZStream.DefaultChunkSize)

Bzip2BlockSize ranges from 1 to 9, in units of 100 KB. Named constants BlockSize100KB to BlockSize900KB exist for each value. A larger block size gives a better compression ratio and needs more memory.

Notesโ€‹

  • Both pipelines fail with a Throwable when the input is not valid Bzip2.
  • Bzip2 compresses one stream at a time. It has no entry names or metadata. To compress several files, use a tar archive inside the Bzip2 stream.

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! ๐Ÿค–