Skip to main content
Version: 2.x

Snappy

The snappy module compresses and decompresses the Snappy format. It is built on the snappy-java library.

Snappy has several incompatible variants. Before you choose a format, read the snappy-java compatibility notes.

Add the module to your build.sbt:

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

SnappyCompressorโ€‹

SnappyCompressor is a Compressor that writes Snappy. It takes a write format. The default is the framed format, and SnappyCompressor.compress uses that default:

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

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

To write the basic format with 64 KiB blocks:

import zio.compress._

val basic = SnappyCompressor(SnappyWriteFormat.Basic(blockSize = 64 * 1024))

SnappyDecompressorโ€‹

SnappyDecompressor is a Decompressor that reads Snappy. It takes a read format. The default is the framed format:

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

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

Write Formatsโ€‹

SnappyWriteFormat has three variants:

VariantOptions and defaults
SnappyWriteFormat.FramedblockSize 64 KiB, must be 1 to 64 KiB. minCompressionRatio 0.85, must be in (0, 1].
SnappyWriteFormat.BasicblockSize 32 KiB, must be 1 KiB to 512 MiB.
SnappyWriteFormat.HadoopCompatibleblockSize 32 KiB, must be 1 KiB to 512 MiB. Writes no file header.

Framed follows the Snappy framing format. The minCompressionRatio is the smallest ratio of compressed to raw size that must be reached before a block is written compressed. Blocks that do not reach it are stored uncompressed.

Basic writes the raw Snappy format, with no framing. HadoopCompatible writes the format of Hadoop and Spark, which puts the block size before each block.

An invalid option, such as a block size out of range, throws an IllegalArgumentException when you create the format.

Read Formatsโ€‹

SnappyReadFormat has two variants:

VariantOptions and defaults
SnappyReadFormat.FramedverifyChecksums true. When true, checksums in the input are verified.
SnappyReadFormat.BasicmaxChunkSize 512 MiB, must be 1 to 512 MiB. The largest number of bytes compressed together.

Read the input with the format it was written with. The formats are not compatible with each other, so a mismatch produces errors or wrong output.

Optionsโ€‹

OptionTypeDefault
formatSnappyWriteFormat (compressor) or SnappyReadFormat (decompressor)Framed()
chunkSizeInt (decompressor only)4 KiB (ZStream.DefaultChunkSize)

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!