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โ
| Option | Type | Default |
|---|---|---|
blockSize | Option[Bzip2BlockSize] | None (900 KB) |
chunkSize | Int (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
Throwablewhen 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.