zlib

    Pure MoonBit zlib/deflate implementation.

    zlib
    deflate
    Download zip
    Author
    Version
    0.4.9
    License
    Apache-2.0
    Last updated
    last month
    Downloads
    191K

    Dependencies

    #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

    mizchi/zlib itself is pure MoonBit on every target, including native.

    If you want the system zlib backend explicitly, import mizchi/zlib/native:

    import "mizchi/zlib/native" as @zlib_native

    let compressed = @zlib_native.zlib_compress(data)
    let decompressed = @zlib_native.zlib_decompress(compressed)

    mizchi/zlib/native links -lz internally, so dependents do not need to add native linker flags themselves.

    # 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)
    OutputTooLarge(String)
    } derive(Eq)

    Errors raised by zlib helpers.

    InvalidData covers malformed inputs (bad headers, checksum mismatches, truncated streams, invalid Huffman codes, etc.).

    OutputTooLarge is raised when decompressed output would exceed the configured max_size. Decompression APIs accept a max_size~ labeled argument; the default is default_max_decompressed_size (256 MiB).
    impl Show for ZlibError

    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

    default_max_decompressed_size

    let default_max_decompressed_size : Int

    Default maximum decompressed output size (256 MiB).

    All public decompression functions enforce this by default to prevent "zip bomb" style DoS where a tiny crafted input decompresses to gigabytes of memory. Override via the max_size~ labeled argument.

    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, max_size? : Int) -> Bytes raise ZlibError

    Decompress a full raw deflate stream.

    deflate_decompress_at

    fn deflate_decompress_at(data : Bytes, start : Int, max_size? : 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, max_size? : Int) -> Bytes raise ZlibError

    Decompress a full gzip stream.

    gzip_decompress_at

    fn gzip_decompress_at(data : Bytes, start : Int, max_size? : 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, max_size? : Int) -> Bytes raise ZlibError

    Decompress a full zlib stream.

    zlib_decompress_at

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

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

    Output is capped at max_size bytes; if exceeded, raises OutputTooLarge.

    zlib_decompress_stored

    fn zlib_decompress_stored(data : Bytes, max_size? : Int) -> Bytes raise ZlibError

    Decompress zlib stored blocks (single stream)

    zlib_decompress_stored_at

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

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

    Output is capped at max_size bytes; if exceeded, raises OutputTooLarge rather than allowing unbounded allocation.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io