High-performance compression library for MoonBit, ported from fflate. (DEFLATE/GZIP/Zlib/ZIP) ๐
moon add hustcer/fzip{
"deps": {
"hustcer/fzip": "0.8.2"
}
}// Compress (GZIP)
let data = @fzip.str_to_u8("Hello, MoonBit!")
let compressed = @fzip.gzip_sync(data)
// Decompress (auto-detect format)
let original = @fzip.decompress_sync(compressed)
let text = @fzip.str_from_u8(original)
println(text) // "Hello, MoonBit!"// Create ZIP archive
let files : Array[(String, FixedArray[Byte])] = [
("hello.txt", @fzip.str_to_u8("Hello!")),
("binary.dat", my_bytes),
]
let archive = @fzip.zip_sync(files)
// Extract ZIP archive
let extracted = @fzip.unzip_sync(archive)
for entry in extracted {
let (filename, content) = entry
println("\{filename}: \{content.length()} bytes")
}
// List files without extracting
let infos = @fzip.unzip_list(archive)
for info in infos {
println("\{info.name}: original size \{info.original_size} (compressed: \{info.size})")
}
// Extract ZIP archive and verify each entry CRC-32
let checked = @fzip.unzip_sync(archive, opts={ verify_checksum: true })let archive = @fzip.zip_sync_checked(files) catch {
FzipError(code~, message~) => {
// Handle Zip64ValueTooLarge, ExtraFieldTooLong,
// FilenameTooLong, InvalidZipData, or another writer error.
return
}
}// compress_sync defaults to gzip_sync
let compressed = @fzip.compress_sync(data)
// decompress_sync automatically detects GZIP, Zlib, or DEFLATE
let original = @fzip.decompress_sync(compressed)let stream = @fzip.DeflateStream::new()
stream.ondata = Some(FlateStreamHandler(fn(data, is_final) {
// handle output chunk
}))
stream.push(chunk1)
stream.push(chunk2, final_=true)// With all security features (default)
let original = @fzip.gunzip_sync(compressed, opts={
out: None,
dictionary: None,
max_output_size: 10485760, // 10MB limit
max_input_size: 104857600, // 100MB limit
verify_checksum: true, // Verify CRC-32 (default)
})
// Skip checksum verification when integrity is checked elsewhere
let original = @fzip.gunzip_sync(compressed, opts={
verify_checksum: false, // Skip CRC-32 for ~4x faster decompression
..
})moon check # Type check
moon build # Full build
moon test # Run tests
moon test -v # Verbose output
moon fmt # Format code
moon bench # Run benchmarksfn DecompressStream::push(self : DecompressStream, chunk : FixedArray[Byte], final_? : Bool) -> Unit raise FzipErrorpub(all) struct DeflateOptions {
level : Int
mem : Int
dictionary : FixedArray[Byte]?
} derive(Debug)pub(all) struct FlateStreamHandler((FixedArray[Byte], Bool) -> Unit)pub(all) enum FzipErrorCode {
UnexpectedEOF
InvalidBlockType
InvalidLengthLiteral
InvalidDistance
StreamFinished
NoStreamHandler
InvalidHeader
NoCallback
InvalidUTF8
ExtraFieldTooLong
InvalidDate
FilenameTooLong
StreamFinishing
InvalidZipData
UnknownCompressionMethod
InvalidChecksum
Zip64ValueTooLarge
} derive(Eq, Debug)impl Show for FzipErrorCodepub(all) struct GunzipOptions {
out : FixedArray[Byte]?
dictionary : FixedArray[Byte]?
max_output_size : Int
max_input_size : Int
verify_checksum : Bool
} derive(Debug)fn GunzipStream::push(self : GunzipStream, chunk : FixedArray[Byte], final_? : Bool) -> Unit raise FzipErrorpub(all) struct GzipOptions {
level : Int
mem : Int
dictionary : FixedArray[Byte]?
mtime : Int
filename : String
} derive(Debug)pub(all) struct InflateOptions {
out : FixedArray[Byte]?
dictionary : FixedArray[Byte]?
max_output_size : Int
max_input_size : Int
} derive(Debug)fn InflateStream::push(self : InflateStream, chunk : FixedArray[Byte], final_? : Bool) -> Unit raise FzipErrorpub(all) struct UnzipFileInfo {
name : String
size : Int
original_size : Int
compression : Int
} derive(Debug)pub(all) struct UnzlibOptions {
out : FixedArray[Byte]?
dictionary : FixedArray[Byte]?
max_output_size : Int
max_input_size : Int
verify_checksum : Bool
} derive(Debug)fn UnzlibStream::push(self : UnzlibStream, chunk : FixedArray[Byte], final_? : Bool) -> Unit raise FzipErrorfn adler32(data : FixedArray[Byte]) -> UIntfn crc32(data : FixedArray[Byte]) -> UIntfn decompress_sync(data : FixedArray[Byte], opts? : InflateOptions) -> FixedArray[Byte] raise FzipErrorlet default_max_input_size : Intlet default_max_output_size : Intlet freq_skew_shift : Intlet full_scan_threshold : Intlet high_entropy_unique_threshold : Intfn inflate_sync(data : FixedArray[Byte], opts? : InflateOptions) -> FixedArray[Byte] raise FzipErrorlet max_filename_length : Intlet min_compressibility_check_len : Intlet periodicity_check_samples : Intlet sampling_entropy_threshold : Intlet sampling_stride : Intlet small_data_mem_threshold : Intfn str_from_u8(data : FixedArray[Byte], latin1? : Bool, offset? : Int, len? : Int) -> String raise FzipErrorfn str_to_u8(s : String, latin1? : Bool) -> FixedArray[Byte]fn unzip_sync(data : FixedArray[Byte], opts? : UnzipOptions) -> Array[(String, FixedArray[Byte])] raise FzipError#alias(zip64_eocd_locator_signature, deprecated="`zip64_eocd_locator_signature` is deprecated, use `zip64_eocd_signature` instead")
let zip64_eocd_signature : UIntlet zip64_locator_signature : UIntfn zip_sync_checked(files : Array[(String, FixedArray[Byte])], opts? : ZipEntryOptions) -> FixedArray[Byte] raise FzipErrorHigh-performance compression library for MoonBit, ported from fflate. (DEFLATE/GZIP/Zlib/ZIP) ๐