Bounded ZIP archives with Store, Deflate and Zstandard writing and Deflate64 reading, in pure MoonBit.
moon add gaato/zip///|
test "create, read, edit and rewrite a ZIP" {
let archive = @zip.Archive()
archive.add("hello.txt", b"hello", compression=Zstandard)
let bytes = @zip.write(archive)
let decoded = @zip.read_all(
bytes,
limits=@zip.ReadLimits(
max_package_bytes=1024 * 1024,
max_entries=100,
max_entry_uncompressed_bytes=1024 * 1024,
max_total_uncompressed_bytes=4 * 1024 * 1024,
),
)
assert_eq(decoded.get("hello.txt"), Some(b"hello"[:]))
assert_true(decoded.replace("hello.txt", b"updated"))
assert_eq(
@zip.read_all(@zip.write(decoded)).get("hello.txt"),
Some(b"updated"[:]),
)
}| Reader | a normal | b garbage | c zeros | d decoy | e decoy + garbage | f SFX | f2 adjusted | g truncated |
|---|---|---|---|---|---|---|---|---|
| gaato/zip, both APIs | R | R | R | R | R | R | R | E |
| Python / unzip / libarchive | R | R | R | 0 | 0 | R | R | R |
| Go / Rust zip | R | R | R | 0 | 0 | R | R | E |
| 7-Zip | R | R | R | R | R | E | R | E |
| Java | R | R | R | 0 | R | R | R | E |
| .NET | R | R | R | 0 | 0 | E | R | E |
| moonbitlang/core | R | E | E | E | E | E | R | E |
| minizip-ng CLI | R | R | R | E | E | R | R | R |
type Archivefn Archive::add(self : Archive, name : String, data : BytesView, compression? : Compression) -> Unittype Directoryfn Directory::open_entry(self : Directory, index : Int, limits? : EntryReadLimits) -> EntryReader raise ZipErrortype DirectoryEntrytype DirectoryLimitsfn DirectoryLimits::DirectoryLimits(max_directory_bytes? : UInt64, max_entries? : UInt64) -> DirectoryLimitstype DirectoryReaderfn DirectoryReader::DirectoryReader(archive_size : UInt64, limits? : DirectoryLimits) -> DirectoryReaderfn DirectoryReader::advance(self : DirectoryReader, input : BytesView) -> DirectoryStep raise ZipErrortype Entrytype EntryReadLimitstype EntryReaderfn EntryReader::step(self : EntryReader, input : BytesView, output : MutArrayView[Byte]) -> EntryStep raise ZipErrorpub struct ReadLimits {
max_package_bytes : Int
max_entries : Int
max_entry_uncompressed_bytes : Int
max_total_uncompressed_bytes : Int
}fn ReadLimits::ReadLimits(max_package_bytes? : Int, max_entries? : Int, max_entry_uncompressed_bytes? : Int, max_total_uncompressed_bytes? : Int) -> ReadLimitstype ReadRangetype Writerfn Writer::add(self : Writer, name : String, data : BytesView, compression? : Compression) -> Unit raisefn Writer::begin_entry(self : Writer, name : String, compression? : Compression, size? : UInt64) -> Unit raisepub(all) enum ZipErrorKind {
Truncated
InvalidSignature
MissingEndOfCentral
Corrupt
UnsupportedCompression(Int)
InvalidUtf8
UnsupportedFeature
Codec(ErrorKind)
InvalidRead(expected~ : Int, actual~ : Int)
InvalidEntryIndex(Int)
SizeMismatch
ChecksumMismatch
LimitExceeded(kind~ : LimitKind, limit~ : UInt64, observed~ : UInt64)
Cancelled
InvalidArgument
InvalidState
} derive(Eq, Debug)fn read_all(bytes : BytesView, limits? : ReadLimits, cancelled? : () -> Bool) -> Archive raise ZipErrorInstall
Download zipBounded ZIP archives with Store, Deflate and Zstandard writing and Deflate64 reading, in pure MoonBit.