Skip to content

std.zip

read and write .zip (stored or DEFLATE); also .docx, .xlsx, .jar, .epub no zip64 (> 4 GB or 65535 entries), encryption or other methods: unsupported

pub ZipError :: errors {
not_zip,
corrupt,
unsupported,
bad_checksum,
}
  • not_zip: no end record: not a zip, or truncated
  • corrupt: record out of bounds or bad signature
  • unsupported: zip64, encryption, or not stored/DEFLATE
  • bad_checksum: CRC-32 or size mismatch
pub Entry :: struct {
name: []const u8,
method: u16,
crc: u32,
compressed_size: u64,
size: u64,
header_offset: u64,
is_dir: bool,
flags: u16,
dos_time: u16,
dos_date: u16,
}

file or folder, as the central directory lists it

  • method: 0 stored, 8 DEFLATE
  • header_offset: where its local header is
  • is_dir: a name that ends in “/”
pub modified :: fn(self: *Self) -> i64

unix seconds from the DOS date/time: local time, 2 s resolution

pub Archive :: struct {
data: []const u8,
entries: []Entry,
}

entries in central-directory order

pub find :: fn(self: *Self, name: []const u8) -> ?Entry
pub read :: fn(self: *Self, a: *Arena, e: Entry) -> []const u8 ! ZipError

contents in a; CRC-32 and size checked

pub open :: fn(a: *Arena, data: []const u8) -> Archive ! ZipError

data is the whole .zip in memory; entries listed in a

pub Writer :: struct {
region: *Arena,
out: Vec(u8),
entries: Vec(Entry),
}

archive being written; finish gives its bytes

pub add :: fn(self: *Self, name: []const u8, data: []const u8, level: u32)

level 0 stores; dated 1980-01-01

pub add_at :: fn(self: *Self, name: []const u8, data: []const u8, level: u32, unix_secs: i64)

zip dates are local, no zone, 2 s steps: pass secs + time::local_offset(secs)

pub add_dir :: fn(self: *Self, name: []const u8)

appends “/” if missing

pub finish :: fn(self: *Self) -> []const u8

added data, then the central directory and end record

pub writer :: fn(a: *Arena) -> Writer

empty archive in a