zlib

Pure MoonBit zlib/deflate implementation.

zlib
deflate
moon add mizchi/zlib@0.4.9
Download zip
Author
Version
0.4.9
License
Apache-2.0
Last updated
22 hours ago
Downloads
184K

Dependencies

README

#mizchi/zlib

Pure MoonBit zlib/deflate implementation. Supports:
  • zlib streams (compress stored / full inflate)
  • raw deflate (stored compress / full inflate)
  • gzip (stored compress / full inflate)

///|
test "roundtrip stored" {
let data : Bytes = "hello"
let compressed = zlib_compress_stored(data)
let decompressed = zlib_decompress_stored(compressed)
inspect(decompressed.length(), content="5")
inspect(decompressed[0], content="b'\\x68'")
inspect(decompressed[4], content="b'\\x6F'")
}

#Native target

On the native target, this library uses system zlib via C FFI for better performance. Dependents need to add -lz to their moon.pkg:

options( link: { "native": { "cc-link-flags": "-lz" } }, )

On js/wasm/wasm-gc targets, the pure MoonBit implementation is used and no system libraries are required.

# Benchmarks moon bench --target js moon bench --target native just bench-compare

#Benchmark (Rust flate2 / miniz_oxide baseline)

Measured on 2026-01-28 with hyperfine --warmup 3 and the bundled 256KiB fixtures. Commands are very short (<5ms), so shell overhead can skew results. wasm/wasm-gc uses moon run, so the runtime/launcher overhead is included.

FormatMoonBit native (mean)Rust baseline (mean)RelativeMoonBit wasm (mean)MoonBit wasm-gc (mean)
zlib3.8 ms2.1 ms (flate2/miniz_oxide)1.8x slower31.8 ms22.9 ms
deflate3.7 ms2.1 ms (flate2/miniz_oxide)1.8x slower30.0 ms22.6 ms
gzip4.2 ms2.2 ms (flate2/miniz_oxide)1.9x slower30.7 ms27.1 ms

#
ZlibError

pub(all) suberror ZlibError {
InvalidData(String)
} derive(Eq, Show)

Errors raised by zlib helpers

#
adler32

fn adler32(data : Bytes) -> Int

Compute Adler-32 checksum

#
adler32_fixed

fn adler32_fixed(data : FixedArray[Byte]) -> Int

Compute Adler-32 checksum from FixedArray

#
crc32

fn crc32(data : Bytes) -> UInt

#
crc32_fixed

fn crc32_fixed(data : FixedArray[Byte]) -> UInt

#
deflate_compress

fn deflate_compress(data : Bytes) -> Bytes

Compress raw deflate stream (best compression).

#
deflate_compress_best

fn deflate_compress_best(data : Bytes) -> Bytes

#
deflate_compress_fixed

fn deflate_compress_fixed(data : Bytes) -> Bytes

#
deflate_compress_stored

fn deflate_compress_stored(data : Bytes) -> Bytes

Compress raw deflate stream using stored blocks (no compression).

#
deflate_decompress

fn deflate_decompress(data : Bytes) -> Bytes raise ZlibError

Decompress a full raw deflate stream.

#
deflate_decompress_at

fn deflate_decompress_at(data : Bytes, start : Int) -> (Bytes, Int) raise ZlibError

Decompress raw deflate stream from the given offset.

#
gzip_compress

fn gzip_compress(data : Bytes) -> Bytes

Compress data using gzip (stored blocks).

#
gzip_compress_stored

fn gzip_compress_stored(data : Bytes) -> Bytes

Compress gzip stream using stored deflate blocks (no compression).

#
gzip_decompress

fn gzip_decompress(data : Bytes) -> Bytes raise ZlibError

Decompress a full gzip stream.

#
gzip_decompress_at

fn gzip_decompress_at(data : Bytes, start : Int) -> (Bytes, Int) raise ZlibError

Decompress gzip stream from the given offset.

#
zlib_compress

fn zlib_compress(data : Bytes) -> Bytes

Compress data using zlib (fixed Huffman).

#
zlib_compress_stored

fn zlib_compress_stored(data : Bytes) -> Bytes

Compress data using zlib stored blocks (no actual compression) Format: CMF(0x78) + FLG(0x01) + blocks + Adler32

#
zlib_decompress

fn zlib_decompress(data : Bytes) -> Bytes raise ZlibError

Decompress a full zlib stream.

#
zlib_decompress_at

fn zlib_decompress_at(data : Bytes, start : Int) -> (Bytes, Int) raise ZlibError

Decompress a zlib stream (deflate with checksum) from the given offset. Returns (decompressed_bytes, next_offset).

#
zlib_decompress_stored

fn zlib_decompress_stored(data : Bytes) -> Bytes raise ZlibError

Decompress zlib stored blocks (single stream)

#
zlib_decompress_stored_at

fn zlib_decompress_stored_at(data : Bytes, start : Int) -> (Bytes, Int) raise ZlibError

Decompress zlib stored blocks from the given offset. Returns (decompressed_bytes, next_offset).

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io