LibdeflateEx

Hex.pmDocs

Note: This is a simple Elixir wrapper over the Rust crate libdeflater. Code was written with Claude Code, however the test suite verifies all functions against their Erlang :zlib counterparts in both directions.

Fast deflate, zlib, and gzip compression/decompression for Elixir, powered by libdeflate via a Rust NIF.

libdeflate is significantly faster than zlib for both compression and decompression, especially for smaller payloads, while achieving comparable or better compression ratios.

Installation

Add libdeflate_ex to your list of dependencies in mix.exs:

def deps do
  [
    {:libdeflate_ex, "~> 0.1.0"}
  ]
end

You need a Rust toolchain installed (via rustup) for the NIF to compile.

Usage

Compression

All compression functions accept a compression level from 1 (fastest) to 12 (highest compression). The default level is 6.

# Deflate (raw, no header)
{:ok, compressed} = LibdeflateEx.deflate_compress("hello world")
{:ok, compressed} = LibdeflateEx.deflate_compress("hello world", 9)

# Zlib (deflate + zlib header/checksum)
{:ok, compressed} = LibdeflateEx.zlib_compress("hello world")

# Gzip
{:ok, compressed} = LibdeflateEx.gzip_compress("hello world")

Decompression

Decompression requires the exact uncompressed size to be known ahead of time. This is common in formats like ZIP where the size is stored in metadata.

original = "hello world"
{:ok, compressed} = LibdeflateEx.deflate_compress(original)
{:ok, ^original} = LibdeflateEx.deflate_decompress(compressed, byte_size(original))

Bang variants

Every function has a ! variant that raises on error instead of returning an error tuple:

compressed = LibdeflateEx.gzip_compress!("hello world")
original = LibdeflateEx.gzip_decompress!(compressed, 11)

Interoperability

LibdeflateEx produces standard-compliant output. Zlib and gzip streams are interoperable with Erlang's :zlib module and other implementations:

# Compress with LibdeflateEx, decompress with Erlang :zlib
{:ok, compressed} = LibdeflateEx.zlib_compress("hello world")
:zlib.uncompress(compressed)
#=> "hello world"

License

Licensed under either of

at your option.