Pure-MoonBit, runtime-agnostic DEFLATE (RFC 1951) engine with gzip/zlib/zip wrappers — suspendable and io-free.
moon add moonbit-community/flate///|
test "README raw DEFLATE round-trip" {
let source = b"runtime-agnostic streaming compression"
let compressed = @flate.deflate_all(source)
@test.assert_eq(@flate.inflate_all(compressed), source)
}///|
test "README caller-buffer DEFLATE" {
let source = b"direct input and caller-owned output"
let compressor = @flate.Compressor(level=6)
let compressed_buffer = FixedArray::make(
@flate.deflate_bound(source.length()),
b'\x00',
)
guard compressor.compress_into(source, compressed_buffer)
is Some(compressed_len) else {
@test.fail("deflate_bound was too small")
}
let compressed = Bytes::from_array(compressed_buffer[:compressed_len])
let decompressor = @flate.Decompressor()
let output = FixedArray::make(source.length(), b'\x00')
let decoded_len = decompressor.decompress_into(compressed, output)
@test.assert_eq(Bytes::from_array(output[:decoded_len]), source)
}///|
test "README prefix decode" {
// A record: a raw DEFLATE stream, then whatever follows it.
let payload = b"the stream ends with its final block"
let record = @flate.deflate_all(payload) + b"what follows"
let decompressor = @flate.Decompressor()
@test.assert_eq(decompressor.decompress(record), payload)
let rest = record[decompressor.last_consumed():]
@test.assert_eq(rest.to_owned(), b"what follows")
}///|
test "README streaming Deflater" {
let source = b"small buffers exercise suspension and resume"
let encoder = @flate.Deflater()
let chunk = FixedArray::make(3, b'\x00')
let compressed = @buffer.Buffer()
let mut first = true
for ;; {
let input = if first { source[:] } else { b"" }
let status = encoder.step(
input,
chunk.mut_view(),
action=if first { Finish } else { Continue },
)
let consumed = encoder.last_consumed()
let produced = encoder.last_produced()
@test.assert_eq(consumed, if first { source.length() } else { 0 })
first = false
for i in 0..<produced {
compressed.write_byte(chunk[i])
}
if status is Done {
break
}
}
@test.assert_eq(@flate.inflate_all(compressed.to_bytes()), source)
}///|
test "README exact and limited inflate" {
let source = b"bounded convenience API"
let compressed = @flate.deflate_all(source)
@test.assert_eq(@flate.inflate_exact(compressed), source)
@test.assert_eq(
@flate.inflate_all(compressed, max_output=source.length()),
source,
)
@test.assert_eq(
@flate.inflate_exact(compressed, max_output=source.length()),
source,
)
}///|
test "README zip round-trip" {
let archive = @zip.Archive()
archive.add("hello.txt", b"hello, zip")
let bytes = @zip.write(archive)
let parsed = @zip.read(bytes)
@test.assert_eq(parsed.get("hello.txt"), Some(b"hello, zip"))
}///|
test "README incremental ZIP writer" {
// A real folder adapter passes a buffered file-write callback here.
let output = @buffer.Buffer()
let writer = @zip.Writer(chunk => output.write_bytes(chunk))
writer.add("small.txt", b"small file")
writer.begin_entry("chunked.txt", size=6UL)
writer.write(b"abc")
writer.write(b"def")
writer.end_entry()
writer.finish()
let archive = @zip.read(output.to_bytes())
@test.assert_eq(archive.get("chunked.txt"), Some(b"abcdef"))
}| Path | Implementation |
|---|---|
| Whole-buffer compression | deflate_all.mbt, lz77.mbt |
| Streaming compression | deflate.mbt, block_planner.mbt, lz77.mbt |
| Content-driven block splitting | split_plan.mbt |
| Iterated optimal parsing and splitting | optimal_parse.mbt, optimal_plan.mbt |
| Block encoding and bit output | block_writer.mbt, huffman_build.mbt, bitwriter.mbt |
| Whole-buffer decompression | inflate_all.mbt |
| Streaming decompression | inflate.mbt |
| Shared decoding tables and validation | huffman_table.mbt, tables.mbt, decode_rules.mbt |
///|
let codec = @flate.Compressor(level=6, fast_store=true)
///|
let raw = @flate.deflate_all(input, fast_store=true)
///|
let stream = @flate.Deflater(level=6, fast_store=true)
///|
let archive_bytes = @zip.write(archive, fast_store=true)pub struct Compressor {
// private fields
}fn Compressor::compress_into(self : Compressor, input : BytesView, output : FixedArray[Byte]) -> Int?pub struct Decompressor {
// private fields
}fn Decompressor::decompress_into(self : Decompressor, input : BytesView, output : FixedArray[Byte], cancelled? : () -> Bool) -> Int raise InflateErrorpub struct Deflater {
// private fields
}fn Deflater::step(self : Deflater, input : BytesView, output : MutArrayView[Byte], action? : DeflateAction) -> Statuspub struct Inflater {
// private fields
}fn Inflater::step(self : Inflater, input : BytesView, output : MutArrayView[Byte], end? : Bool) -> Status raise InflateErrorfn Inflater::step_into(self : Inflater, input : BytesView, output : FixedArray[Byte], end? : Bool) -> Status raise InflateErrorfn deflate_all(input : BytesView, level? : Int, fast_store? : Bool) -> Bytesfn deflate_all_optimal(input : BytesView, iterations? : Int, fast_store? : Bool) -> Bytesfn deflate_all_split(input : BytesView, level? : Int, fast_store? : Bool) -> Bytesfn deflate_bound(input_size : Int) -> Intfn deflate_into(input : BytesView, output : FixedArray[Byte], level? : Int, fast_store? : Bool) -> Int?fn inflate_all(input : BytesView, max_output? : Int, cancelled? : () -> Bool) -> Bytes raise InflateErrorfn inflate_exact(input : BytesView, max_output? : Int, cancelled? : () -> Bool, preallocated? : Bool) -> Bytes raise InflateErrorfn inflate_into(input : BytesView, output : FixedArray[Byte], cancelled? : () -> Bool) -> Int raise InflateErrorInstall
Download zipPure-MoonBit, runtime-agnostic DEFLATE (RFC 1951) engine with gzip/zlib/zip wrappers — suspendable and io-free.