Thin Lean 4 FFI bindings to system zlib.
Whole-buffer and streaming compression/decompression for the zlib (RFC 1950), gzip (RFC 1952), and raw DEFLATE (RFC 1951) formats, plus CRC-32 and Adler-32 checksums. This is the fast, ubiquitous C baseline; if you want the formally verified pure-Lean DEFLATE codec instead, see lean-zip.
Extracted from lean-zip
(pre-split tag), where these bindings served as the conformance reference
for the verified codec.
Add to your lakefile.lean:
require "kim-em" / "lean-zlib"import Zlib
-- Zlib format
let compressed ← Zlib.compress data
let original ← Zlib.decompress compressed
-- Gzip format (compatible with gzip/gunzip)
let gzipped ← Gzip.compress data (level := 6)
let original ← Gzip.decompress gzipped
-- Raw deflate (no header/trailer, used internally by ZIP)
let deflated ← RawDeflate.compress data
let original ← RawDeflate.decompress deflatedAll decompression entry points default to a 1 GiB output cap; pass
maxDecompressedSize := 0 to opt into unlimited mode (bomb-unsafe for
untrusted input). See SECURITY.md.
For data too large to fit in memory:
-- Stream between IO.FS.Streams (64KB chunks, bounded memory)
Gzip.compressStream inputStream outputStream (level := 6)
Gzip.decompressStream inputStream outputStream
-- File helpers
let gzPath ← Gzip.compressFile "/path/to/file" -- writes /path/to/file.gz
let outPath ← Gzip.decompressFile "/path/to/file.gz" -- writes /path/to/filelet state ← Gzip.DeflateState.new (level := 6)
let compressed ← state.push chunk1
let compressed2 ← state.push chunk2
let final ← state.finish -- must call exactly oncelet crc := Checksum.crc32 0 data -- CRC-32
let adler := Checksum.adler32 1 data -- Adler-32
-- Incremental: pass previous result as init
let crc2 := Checksum.crc32 crc moreData- Lean toolchain per
lean-toolchain(via elan) - system zlib and
pkg-config(Ubuntu:apt install libz-dev pkg-config; macOS: ships with the SDK; NixOS:nix-shelluses the providedshell.nix) - To override probing: set
ZLIB_CFLAGS/ZLIB_LDFLAGS
lake build
lake exe test
scripts/sanitize-ffi.sh rebuilds the FFI under ASan + UBSan and runs the
test suite; scripts/check-c-allocations.sh is an advisory trip wire for new
allocation sites in c/zlib_ffi.c (see SECURITY.md).
Apache 2.0. Test fixtures from other projects are used under their own licenses; see testdata/LICENSES.md.