package core:archive/zip
Overview
Reads, writes and updates ZIP archives, as specified by version 6.3.10 (November 2022) of PKWARE's APPNOTE.TXT.
open reads an archive from a path, a byte slice or a seekable io.Stream, self-extracting
ones included. create starts a new archive at a path or on an io.Stream, write_entry adds
a member from a byte slice or an io.Reader, write_directory adds a whole tree, and commit
finishes the archive. update appends members to an existing archive in place; at a path,
remove_entry and rename_entry rewrite it. close releases a reader or a writer. A crash
during an update at a path leaves the last committed archive, or the one before. At a path,
readers take a shared lock and writers an exclusive one, except on wasi.
Members are read through an iterator: iterator_create walks them in storage order, iterate
gives each one's archive.File_Info, and extract_current unpacks it to an io.Writer, or
open_current streams it through an io.Reader. find looks a member up by path for
extract or open_member, and core:archive's extract_all unpacks a whole archive below a
directory. Extraction checks each member's CRC-32, and several threads can read one archive at
once.
Reading decodes Store, Shrink, Reduce, Implode, Deflate, Deflate64, bzip2, LZMA, PPMd and XZ.
Writing encodes Store, Deflate, Deflate64, bzip2, LZMA, PPMd and XZ, and falls back to Store
when compressing doesn't pay. Reading decrypts ZipCrypto and WinZip AES (AE-1 and AE-2, with
128, 192 or 256-bit keys), with the password from archive.Read_Options, and checks the MAC;
writing encrypts with WinZip AES-256 (AE-2).
ZIP64 and data descriptors are used where needed. Names are read as UTF-8, Info-ZIP Unicode or code page 437, and written as UTF-8. Unix and DOS attributes, symlinks, NTFS and Unix timestamps, and comments are read and written; DOS times are taken as local time.
Reading and writing take an optional progress callback, for a UI's progress bar, which can also cancel. A deterministic mode writes reproducible archives.
Planned: copy-on-write updates, safe alongside concurrent readers.
Not supported: archives split over several disks, PKWARE's strong encryption and masked headers, and methods beyond those above, such as Zstandard. Shrink, Reduce, Implode and ZipCrypto are read but never written. On js, only byte slices and streams work.
Index
Constants (2)
Variables (0)
This section is empty.
Procedures (31)
close_… (3)
- commit
- create_from_path
- create_from_stream
- current_entry
- find
- info
- iterate
iterator_… (3)
- next_extra
- read_link
- remove_entry
- rename_entry
- update_from_path
- update_from_stream
write_… (3)
Procedure Groups (7)
Types
Entry ¶
Entry :: struct { flags: bit_set[Flag; u16le], // The general purpose bit flags method: Method, // The method as stored; `.AES` for a WinZip AES member host: Host, // The host system in "version made by" has_local: bool, // `false` if the local header couldn't be read or doesn't match name: []u8, // The name's bytes, as stored central_extra: []u8, // The central header's extra fields local_extra: []u8, // The local header's extra fields; empty without `has_local` comment: []u8, // The comment's bytes, as stored header_offset: i64, // Where the local header starts data_offset: i64, }
A member's format details, from current_entry; walk the extra fields with next_extra
Entry_Data ¶
A regular file's data, for write_entry
Related Procedures With Parameters
- write_entry_simple
- write_entry_with_info
- write_entry (procedure groups)
Entry_Options ¶
Entry_Options :: struct { method: archive.Method, // `.Unknown` for the writer's level: runtime.Maybe($T=int), // `nil`: the writer's for its method, else the default password: runtime.Maybe($T=string), // `nil` for the writer's, `""` for none replace: bool, }
One member's settings; all but replace stand in for the Writer_Options defaults
Related Procedures With Parameters
- write_directory
- write_entry_simple
- write_entry_with_info
- write_entry (procedure groups)
Error ¶
Error :: archive.Error
Related Procedures With Returns
- commit
- create_from_path
- create_from_stream
- extract_current_to_dynamic
- extract_current_to_writer
- extract_to_dynamic
- extract_to_writer
- find
- iterator_error
- open_current
- open_from_bytes
- open_from_path
- open_from_stream
- open_member
- read_link
- remove_entry
- rename_entry
- update_from_path
- update_from_stream
- write_directory
- write_entry_simple
- write_entry_with_info
- create (procedure groups)
- extract (procedure groups)
- extract_current (procedure groups)
- open (procedure groups)
- update (procedure groups)
- write_entry (procedure groups)
Extra_ID ¶
Extra_ID :: enum u16 { Zip64 = 0x0001, // 4.5.3 NTFS = 0x000a, // 4.5.5 Timestamp = 0x5455, // 4.6.1; Info-ZIP's extrafld.txt Infozip_Unix = 0x5855, // 4.6.1; Info-ZIP's extrafld.txt Unicode_Path = 0x7075, // 4.6.9 Unix = 0x7875, // 4.6.1; Info-ZIP's extrafld.txt AES = 0x9901, // 4.6.1 and APPENDIX E }
Extra field header IDs, APPNOTE v6.3.10 sections 4.5.2 and 4.6.1.
Flag ¶
Flag :: enum u16 { Encrypted = 0, Option_1 = 1, Option_2 = 2, Data_Descriptor = 3, Enhanced_Deflate = 4, Patched = 5, Strong_Encryption = 6, UTF8 = 11, Enhanced_Compression = 12, Masked_Headers = 13, Alternate_Streams = 14, }
General purpose bit flags, APPNOTE v6.3.10 section 4.4.4: bits 1 and 2 depend on the method.
Host ¶
Host :: enum u8 { MS_DOS = 0, Amiga = 1, OpenVMS = 2, Unix = 3, VM_CMS = 4, Atari_ST = 5, OS2_HPFS = 6, Macintosh = 7, Z_System = 8, CPM = 9, NTFS = 10, MVS = 11, VSE = 12, Acorn_RISC = 13, VFAT = 14, Alternate_MVS = 15, BeOS = 16, Tandem = 17, OS400 = 18, Darwin = 19, }
Hosts in "version made by", APPNOTE v6.3.10 section 4.4.2.2.
Iterator ¶
Iterator :: struct { reader: ^Reader, position: int, index: int, entry: Entry, local_buf: [dynamic]u8, member: Member, }
Walks a Reader's members, from iterator_create
Related Procedures With Parameters
Method ¶
Method :: enum u16le { Store = 0, // 4.4.5 Shrink = 1, // 5.1 Reduce_1 = 2, // 5.2: methods 2-5 are compression factors 1-4 Reduce_2 = 3, // 5.2 Reduce_3 = 4, // 5.2 Reduce_4 = 5, // 5.2 Implode = 6, // 5.3 Tokenize = 7, // 5.4: reserved Deflate = 8, // 5.5 Deflate64 = 9, // 5.6 DCL_Implode = 10, // 4.4.5: PKWARE Data Compression Library Imploding BZip2 = 12, // 5.7 LZMA = 14, // 5.8 IBM_CMPSC = 16, // 5.17 IBM_TERSE = 18, // 4.4.5 IBM_LZ77 = 19, // 4.4.5 Zstandard_Deprecated = 20, // 4.4.5: use method 93 Zstandard = 93, // 4.4.5 MP3 = 94, // 4.4.5 XZ = 95, // 4.4.5 JPEG = 96, // 4.4.5 WavPack = 97, // 5.9 PPMd = 98, // 5.10: PPMd version I, revision 1 AES = 99, // 5.11 and APPENDIX E: AE-x encryption marker }
Compression methods, listed in APPNOTE v6.3.10 section 4.4.5 and described in section 5.
Reader ¶
Reader :: struct { allocator: runtime.Allocator, options: compress.Options, store: Store, file: io.Stream, directory: []u8, owns_dir: bool, records: []Record, order: []int, names: []u8, index: []Name_Index, comment: string, raw_comment: []u8, base: i64, dir_start: i64, zip64: bool, generation: u32, zone: ^datetime.TZ_Region, }
Reads a zip archive, from open
Related Procedures With Parameters
- close_reader
- extract_to_dynamic
- extract_to_writer
- find
- info
- iterator_create
- open_from_bytes
- open_from_path
- open_from_stream
- open_member
- close (procedure groups)
- extract (procedure groups)
- open (procedure groups)
Writer ¶
Writer :: struct { allocator: runtime.Allocator, options: Writer_Options, mode: Writer_Mode, stream: io.Stream, disk: archive.Disk, seekable: bool, offset: i64, end: i64, base: i64, count: i64, archive_in: i64, central: [dynamic]u8, header: [dynamic]u8, name: [dynamic]u8, chunk: []u8, cipher_buffer: []u8, password: string, err: archive.Error, committed: bool, active: bool, zone: ^datetime.TZ_Region, old: Reader, old_length: i64, old_count: i64, slots: [dynamic]Update_Slot, heads: map[string]int, journaled: bool, modified: bool, comment_set: bool, }
Related Procedures With Parameters
- close_writer
- commit
- create_from_path
- create_from_stream
- remove_entry
- rename_entry
- update_from_path
- update_from_stream
- write_directory
- write_entry_simple
- write_entry_with_info
- close (procedure groups)
- create (procedure groups)
- update (procedure groups)
- write_entry (procedure groups)
Writer_Options ¶
Writer_Options :: struct { method: archive.Method, // `.Unknown` for Deflate level: runtime.Maybe($T=int), // `nil` for the method's default; clamped deterministic: bool, // Reproducible: fixed times, sorted `write_directory` comment: string, // The archive comment, at most 65535 bytes password: string, // Encrypts members with AES-256 (AE-2); not `deterministic` no_sync: bool, options: compress.Options, }
A writer's settings; the methods are Store, Deflate, Deflate64, BZip2, LZMA, XZ and PPMd_I
Related Procedures With Parameters
- create_from_path
- create_from_stream
- update_from_path
- update_from_stream
- create (procedure groups)
- update (procedure groups)
Zip_Error ¶
Zip_Error :: archive.Zip_Error
Constants
ONE_SHOT_MAX ¶
ONE_SHOT_MAX :: i64(#config(ZIP_ONE_SHOT_MAX, 256 * 1024))
Members up to this size are compressed in one call; a reader of known size is read in first
Variables
This section is empty.
Procedures
close_member ¶
close_member :: proc(reader: io.Stream) {…}
close_reader ¶
close_reader :: proc(r: ^Reader) {…}
Closes a reader and frees its memory
Inputs:
- r The reader; a stream given to
open_from_streamis left open
close_writer ¶
close_writer :: proc(zw: ^Writer) {…}
Closes a writer and frees its memory
Without commit, an update is rolled back and a new archive at a path is discarded; a new
archive on a stream is left unfinished.
Inputs:
- zw The writer; a stream given to
create_from_streamorupdate_from_streamis left open
commit ¶
Finishes the archive: writes the central directory and, at a path, puts the archive in place
Committing again does nothing. Afterwards, only close is left to call.
In an update, appending or changing the comment fails with Zip_Error.Truncated when an old
member's data runs past the archive's end; at a path, removing those members first works.
Inputs:
- zw An open writer
Returns:
- err The error that stopped the writer earlier, the commit's own error, or
nil
create_from_path ¶
create_from_path :: proc( zw: ^Writer, path: string, options: Writer_Options = {}, allocator := context.allocator, ) -> (err: archive.Error) {…}
Starts a new zip archive at a path
The archive is written to path plus archive.TEMP_SUFFIX, which commit renames over
path. An existing archive at path is locked meanwhile, except on wasi.
Allocates Using Provided Allocator
Inputs:
- zw The writer;
closeit when done, it is left closed on an error - path Where the archive goes
- options The default method, level and password, the archive comment, and more (default: {})
- allocator (default: context.allocator)
Returns:
- err Bad options as for
create_from_stream,General_Error.Busyif another process holds the archive, an OS error, ornil
create_from_stream ¶
create_from_stream :: proc( zw: ^Writer, s: io.Stream, options: Writer_Options = {}, allocator := context.allocator, ) -> (err: archive.Error) {…}
Starts a new zip archive on a stream
Allocates Using Provided Allocator
Inputs:
- zw The writer;
closeit when done, it is left closed on an error - s Where the archive goes, from its current position; if it can't seek, members get data descriptors
- options The default method, level and password, the archive comment, and more (default: {})
- allocator (default: context.allocator)
Returns:
- err
General_Error.Incompatible_OptionsorGeneral_Error.Unsupported_Methodfor bad options,Zip_Error.Unsupported_Password,io.Error.Unsupportedifscan't write, ornil
current_entry ¶
Returns the current member's format details, for what File_Info leaves out
Inputs:
- it The iterator, on the member
iteratereturned last
Returns:
extract_current_to_dynamic ¶
extract_current_to_dynamic :: proc( it: ^Iterator, output: ^[dynamic]u8, options: archive.Read_Options = archive.Read_Options{}, ) -> (err: archive.Error) {…}
Reads the current member's data into a buffer, decompressed and checked
Inputs:
- it The iterator, on the member
iteratereturned last - output Replaced by the member's data, grown with its own allocator; emptied on an error
- options The member's password, if encrypted (default: {})
Returns:
- err
General_Error.No_Current_Memberifiteratehasn't returned a member, an allocator error, the member's error, ornil
extract_current_to_writer ¶
extract_current_to_writer :: proc( it: ^Iterator, w: io.Stream, options: archive.Read_Options = archive.Read_Options{}, ) -> (err: archive.Error) {…}
Writes the current member's data to a writer, decompressed and checked
Inputs:
- it The iterator, on the member
iteratereturned last - w Where the member's data is written
- options The member's password, if encrypted (default: {})
Returns:
- err
General_Error.No_Current_Memberifiteratehasn't returned a member, or the member's error, ornil
extract_to_dynamic ¶
extract_to_dynamic :: proc( r: ^Reader, handle: archive.Handle, output: ^[dynamic]u8, options: archive.Read_Options = archive.Read_Options{}, ) -> (err: archive.Error) {…}
Reads a member's data into a buffer, decompressed and checked
Inputs:
- r An open reader
- handle The member, from
File_Info.handleorfind - output Replaced by the member's data, grown with its own allocator; emptied on an error
- options The member's password, if encrypted (default: {})
Returns:
- err
General_Error.Not_Foundfor a stale handle, an allocator error, the member's error, ornil
extract_to_writer ¶
extract_to_writer :: proc( r: ^Reader, handle: archive.Handle, w: io.Stream, options: archive.Read_Options = archive.Read_Options{}, ) -> (err: archive.Error) {…}
Writes a member's data to a writer, decompressed and checked
Inputs:
- r An open reader
- handle The member, from
File_Info.handleorfind - w Where the member's data is written
- options The member's password, if encrypted (default: {})
Returns:
- err
General_Error.Not_Foundfor a stale handle, the member's error, ornil
find ¶
find :: proc( r: ^Reader, path: string, ) -> (fi: archive.File_Info, handle: archive.Handle, ok: bool, err: archive.Error) {…}
Finds a member by path
NOTE: Of several members with the path, the first in storage order is found.
Inputs:
- r An open reader
- path The member's path, ignoring a leading
./or trailing/
Returns:
- fi The member's info; its strings last until
close - handle The member, for
extractandopen_member - ok
falseif no member has the path - err Always
nil: the directory was read at open
info ¶
info :: proc(r: ^Reader) -> (info: archive.Archive_Info) {…}
Summarizes an archive
Inputs:
- r An open reader
Returns:
- info Its format, member count and comment;
compressionis.Unknown, as it varies by member
iterate ¶
Moves to the next member
Inputs:
- it The iterator
Returns:
- fi The member's info; its strings last until
close - index The member's index, as in
fi.handle: its position in the central directory - ok
falseafter the last member
iterator_create ¶
Creates an iterator over an archive's members, in storage order
Inputs:
- r An open reader
Returns:
- it The iterator; destroy it with
iterator_destroy
iterator_destroy ¶
iterator_destroy :: proc(it: ^Iterator) {…}
iterator_error ¶
Tells why iteration stopped
Inputs:
- it The iterator
Returns:
- path Always empty
- err Always
nil: the directory was read at open
next_extra ¶
Splits the next extra field off a header's extra data
Inputs:
- extra The extra data, such as
Entry.central_extra; advanced past the field
Returns:
- id The field's header ID
- data The field's data, a slice of
extra - ok
falseat the end, or if the next field is cut short
open_current ¶
open_current :: proc( it: ^Iterator, options: archive.Read_Options = archive.Read_Options{}, ) -> (reader: io.Stream, err: archive.Error) {…}
Opens the current member's data as a reader, which decompresses as it is read
Inputs:
- it The iterator, on the member
iteratereturned last - options The member's password, if encrypted (default: {})
Returns:
open_from_bytes ¶
open_from_bytes :: proc( r: ^Reader, data: []u8, options: compress.Options = compress.Options{}, allocator := context.allocator, ) -> (err: archive.Error) {…}
Opens a zip archive held in memory
Allocates Using Provided Allocator
Inputs:
- r The reader;
closeit when done, it is left closed on an error - data The archive's bytes; they must stay valid until
close - options Limits and progress callback for the decoders, see
compress.Options(default: {}) - allocator (default: context.allocator)
Returns:
- err A
Zip_Errorfor a malformed archive, ornil
open_from_path ¶
open_from_path :: proc( r: ^Reader, path: string, options: compress.Options = compress.Options{}, allocator := context.allocator, ) -> (err: archive.Error) {…}
Opens a zip archive file
The file is share-locked until close, except on wasi. After a crashed update, the reader sees
the last committed state, as the update's journal records it.
Allocates Using Provided Allocator
Inputs:
- r The reader;
closeit when done, it is left closed on an error - path The archive's path
- options Limits and progress callback for the decoders, see
compress.Options(default: {}) - allocator (default: context.allocator)
Returns:
- err
General_Error.Busywhile a writer holds the file, an OS error, aZip_Errorfor a malformed archive, ornil
open_from_stream ¶
open_from_stream :: proc( r: ^Reader, s: io.Stream, options: compress.Options = compress.Options{}, allocator := context.allocator, ) -> (err: archive.Error) {…}
Opens a zip archive from a stream
Allocates Using Provided Allocator
Inputs:
- r The reader;
closeit when done, it is left closed on an error - s The archive; it must read at an offset or seek, report its size, and stay open until
close, which leaves it open - options Limits and progress callback for the decoders, see
compress.Options(default: {}) - allocator (default: context.allocator)
Returns:
- err
General_Error.Not_Seekable, aZip_Errorfor a malformed archive, ornil
open_member ¶
open_member :: proc( r: ^Reader, handle: archive.Handle, options: archive.Read_Options = archive.Read_Options{}, allocator := context.allocator, ) -> (reader: io.Stream, err: archive.Error) {…}
Opens a member's data as a reader, which decompresses as it is read
Allocates Using Provided Allocator
Inputs:
- r An open reader
- handle The member, from
File_Info.handleorfind - options The member's password, if encrypted (default: {})
- allocator (default: context.allocator)
Returns:
- reader The member's reader; close it with
close_member - err
General_Error.Not_Foundfor a stale handle, the member's error, ornil
read_link ¶
read_link :: proc( it: ^Iterator, options: archive.Read_Options = archive.Read_Options{}, allocator := context.allocator, ) -> (target: string, err: archive.Error) {…}
Reads the current member's link target, which zip stores as the member's data
Allocates Using Provided Allocator
Inputs:
- it The iterator, on the member
iteratereturned last - options The member's password, if encrypted (default: {})
- allocator (default: context.allocator)
Returns:
- target The symlink's target, empty for other members;
deleteit - err
General_Error.No_Current_Memberifiteratehasn't returned a member,General_Error.Limit_Exceededpastarchive.LINK_TARGET_MAX, the member's error, ornil
remove_entry ¶
Removes a member, taking effect at commit
Inputs:
- zw A writer from
update_from_path - path The member's path, ignoring a leading
./or trailing/; every member with it goes
Returns:
- err
General_Error.Incompatible_Optionsfor a writer from elsewhere,General_Error.Not_Found, ornil
rename_entry ¶
Renames a member, taking effect at commit
Inputs:
- zw A writer from
update_from_path - old_path The member's path, ignoring a leading
./or trailing/; every member with it is renamed - new_path Its new path
Returns:
- err
General_Error.Incompatible_Optionsfor a writer from elsewhere,General_Error.Not_Found,General_Error.Existsifnew_pathis taken, ornil
update_from_path ¶
update_from_path :: proc( zw: ^Writer, path: string, options: Writer_Options = {}, allocator := context.allocator, ) -> (err: archive.Error) {…}
Opens a zip archive at a path to append, replace, remove or rename members
Appends happen in place, under a journal that rolls them back if the process dies before
commit. A replacement, remove or rename makes commit write a new archive and rename it over
the old one. The archive is locked until close, except on wasi.
Allocates Using Provided Allocator
Inputs:
- zw The writer;
closeit when done, it is left closed on an error - path The archive's path
- options The defaults for new members, and more; an empty comment keeps the archive's (default: {})
- allocator (default: context.allocator)
Returns:
- err Bad options as for
create_from_stream,General_Error.Busyif another process holds the archive, an OS error, aZip_Errorfor a malformed archive, ornil
update_from_stream ¶
update_from_stream :: proc( zw: ^Writer, s: io.Stream, options: Writer_Options = {}, allocator := context.allocator, ) -> (err: archive.Error) {…}
Opens a zip archive on a stream to append members in place
Allocates Using Provided Allocator
Inputs:
- zw The writer;
closeit when done, it is left closed on an error - s The archive; it must read, write and seek
- options The defaults for new members, and more; an empty comment keeps the archive's (default: {})
- allocator (default: context.allocator)
Returns:
- err
General_Error.Not_Seekable, bad options as forcreate_from_stream, aZip_Errorfor a malformed archive, ornil
write_directory ¶
write_directory :: proc( zw: ^Writer, dir: string, prefix: string = "", options: Entry_Options = {}, ) -> (err: archive.Error) {…}
Adds a directory tree: its directories, regular files and symlinks
Other file types are skipped. With deterministic, members go in sorted order.
Inputs:
- zw An open writer
- dir The directory to add
- prefix Prepended to every member path; when set,
diritself is added, namedprefix(default: "") - options Every member's method, level and password, in place of the writer's, and whether they replace the members an archive being updated has at their paths (default: {})
Returns:
- err The first error, from the walk or from
write_entry_with_info, ornil
write_entry_simple ¶
write_entry_simple :: proc( zw: ^Writer, name: string, data: Entry_Data = nil, options: Entry_Options = {}, ) -> (err: archive.Error) {…}
Adds a regular file, stamped with the current time
Inputs:
- zw An open writer
- name The member's path
- data The file's data, as bytes or a reader (default: nil)
- options This member's method, level and password, in place of the writer's (default: {})
Returns:
- err As
write_entry_with_info
write_entry_with_info ¶
write_entry_with_info :: proc( zw: ^Writer, fi: archive.File_Info, data: Entry_Data = nil, options: Entry_Options = {}, ) -> (err: archive.Error) {…}
Adds a member described by a File_Info
An error after part of the member was written stops the writer: later calls return it.
Inputs:
- zw An open writer
- fi The member's path, type and metadata; a regular file, a directory or a symlink.
sizemay be -1 for a reader of unknown size - data A regular file's data, as bytes or a reader;
nilfor other types (default: nil) - options This member's method, level and password, in place of the writer's, and whether it replaces the members an archive being updated has at its path (default: {})
Returns:
- err
General_Error.Unsupported_Entry,General_Error.Incompatible_Optionsfor data on a non-file or for replacing a member in an update on a stream,General_Error.Existsfor a path an update already holds, unlessreplaceis set and the archive held it when opened,Zip_Error.Size_Mismatchif the reader doesn't givefi.sizebytes, ornil
Procedure Groups
create ¶
create :: proc{ create_from_path, create_from_stream, }
Starts a new zip archive at a path or on a stream
extract ¶
extract :: proc{ extract_to_writer, extract_to_dynamic, }
Extracts a member's data to a writer or into a buffer
extract_current ¶
extract_current :: proc{ extract_current_to_writer, extract_current_to_dynamic, }
Extracts the current member's data to a writer or into a buffer
open ¶
open :: proc{ open_from_path, open_from_bytes, open_from_stream, }
Opens a zip archive from a path, a byte slice or a stream
update ¶
update :: proc{ update_from_path, update_from_stream, }
Opens a zip archive at a path or on a stream for changes
write_entry ¶
write_entry :: proc{ write_entry_with_info, write_entry_simple, }
Adds a member described by a File_Info, or a regular file by name
Source Files
Generation Information
Generated with odin version dev-2026-11 (vendor "odin") Windows_amd64 @ 2026-11-28 21:05:09.772358800 +0000 UTC