package core:archive/tar
Overview
Reads and writes tar archives: v7, ustar and pax as specified by POSIX.1-2024, and GNU tar's.
open reads an archive from a path, a byte slice or any io.Stream; one that can't seek, such
as a pipe, is read in a single forward pass. gzip, bzip2, LZ4 (frame), LZMA (.lzma) and XZ
compression are detected from the content. create starts a new archive at a path or on any
io.Stream, as pax (the default), ustar or GNU, compressed with gzip, bzip2, LZ4, LZMA or XZ,
or not at all. write_entry adds a member from a byte slice or an io.Reader, reading one of
unknown size into memory first, write_directory adds a whole tree, and commit finishes the
archive. close releases a reader or a writer.
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. The reader has one position, shared by its iterators, find, extract and
open_member.
Reading takes v7, ustar, GNU and star headers, pax global and local records, GNU long names and link targets, and GNU and pax sparse files, whose holes read as zeros. Members can be regular files, directories, symlinks, hard links, devices and named pipes.
Writing takes an optional progress callback, for a UI's progress bar, which can also cancel; reading a compressed archive reports its decoder's progress the same way. A deterministic mode writes reproducible archives.
Not supported: updating an archive in place, GNU multi-volume archives, writing sparse members, and encryption, which tar doesn't have. On js, only byte slices and streams work.
Index
Constants (2)
Variables (0)
This section is empty.
Procedures (26)
Procedure Groups (6)
Types
Entry ¶
Entry :: struct { block: [512]u8, // The header block as stored format: Header_Format, // The block's layout, told by its magic typeflag: u8, // The type flag, as in `block[156]` global_records: []u8, // The pax global records stored just before the member local_records: []u8, }
A member's raw header, from current_entry; walk its pax records with next_record
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)
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
- write_directory
- write_entry_simple
- write_entry_with_info
- create (procedure groups)
- extract (procedure groups)
- extract_current (procedure groups)
- open (procedure groups)
- write_entry (procedure groups)
Header_Format ¶
Header_Format :: enum u8 { V7, // No magic: Unix V7 Ustar, // POSIX ustar, which pax extends GNU, // GNU tar's Star, // Schily's star, with a shorter path prefix }
A header's layout, told by its magic
Reader ¶
Reader :: struct { allocator: runtime.Allocator, options: compress.Options, compression: archive.Method, source: Source, input: io.Stream, input_data: []u8, input_start: i64, input_size: i64, seekable: bool, file: io.Stream, head: [1024]u8, prefix: Prefix_Stream, ctx: compress.Context, inflate: compress_deflate.Reader, bzip2_reader: compress_bzip2.Reader, lz4_reader: compress_lz4.Frame_Reader, lzma_reader: compress_lzma.Reader, xz_reader: compress_xz.Reader, data: []u8, buf: []u8, pos: int, end: int, stream: io.Stream, offset: i64, index: int, remaining: i64, padding: i64, ended: bool, member_count: int, header: Entry, local_buf: [dynamic]u8, global_buf: [dynamic]u8, globals: [Global_Key][dynamic]u8, global_set: bit_set[Global_Key], gnu_sparse: bool, path_buf: [dynamic]u8, long_name: [dynamic]u8, long_link: [dynamic]u8, has_long: bit_set[Long_Kind], label: [dynamic]u8, has_label: bool, sparse: bool, sparse_map: [dynamic]Sparse_Region, sparse_index: int, sparse_pos: i64, data_size: i64, current: archive.File_Info, has_current: bool, unsupported: archive.Error, broken: archive.Error, pending: archive.Error, fail_path: [dynamic]u8, }
Reads a tar 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)
Tar_Error ¶
Tar_Error :: archive.Tar_Error
Write_Format ¶
Write_Format :: enum u8 { Pax, // Ustar, with pax records for what ustar can't hold Ustar, // Plain ustar; what it can't hold is an error GNU, // GNU tar's, with long paths as extra members }
The header layout a writer produces
Writer ¶
Writer :: struct { allocator: runtime.Allocator, options: Writer_Options, stream: io.Stream, out: io.Stream, disk: archive.Disk, deflate: compress_deflate.Writer, bzip2: compress_bzip2.Writer, lz4: compress_lz4.Frame_Writer, lzma: compress_lzma.Writer, xz: compress_xz.Writer, block: [512]u8, records: [dynamic]u8, name: [dynamic]u8, chunk: []u8, written: i64, count: i64, archive_in: i64, err: archive.Error, committed: bool, active: bool, }
Writes a tar archive, from create
Related Procedures With Parameters
- close_writer
- commit
- create_from_path
- create_from_stream
- write_directory
- write_entry_simple
- write_entry_with_info
- close (procedure groups)
- create (procedure groups)
- write_entry (procedure groups)
Writer_Options ¶
Writer_Options :: struct { compression: archive.Method, // The whole archive's; `.Unknown` for Store level: runtime.Maybe($T=int), // `nil` for the method's default; else in its range format: Write_Format, deterministic: bool, // Reproducible: zero owners and times, sorted `write_directory` no_sync: bool, options: compress.Options, }
A writer's settings; compression is Store, Deflate (gzip), BZip2, LZ4, LZMA or XZ
Related Procedures With Parameters
- create_from_path
- create_from_stream
- create (procedure groups)
Constants
BLOCK_SIZE ¶
BLOCK_SIZE :: 512
HEADER_RECORD_MAX ¶
HEADER_RECORD_MAX :: 1 << 20
The most bytes of pax records, long names and sparse maps one member may carry
Variables
This section is empty.
Procedures
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(tw: ^Writer) {…}
Closes a writer and frees its memory
Without commit, a new archive at a path is discarded; one on a stream is left unfinished.
Inputs:
- tw The writer; a stream given to
create_from_streamis left open
commit ¶
Finishes the archive: writes the end marker and, at a path, puts the archive in place
Committing again does nothing. Afterwards, only close is left to call.
Inputs:
- tw 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( tw: ^Writer, path: string, options: Writer_Options = {}, allocator := context.allocator, ) -> (err: archive.Error) {…}
Starts a new tar 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:
- tw The writer;
closeit when done, it is left closed on an error - path Where the archive goes
- options The compression and its level, the header format, 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( tw: ^Writer, s: io.Stream, options: Writer_Options = {}, allocator := context.allocator, ) -> (err: archive.Error) {…}
Starts a new tar archive on a stream
Allocates Using Provided Allocator
Inputs:
- tw The writer;
closeit when done, it is left closed on an error - s Where the archive goes, from its current position; it need not seek
- options The compression and its level, the header format, and more (default: {})
- allocator (default: context.allocator)
Returns:
- err
General_Error.Incompatible_OptionsorGeneral_Error.Unsupported_Methodfor bad options,io.Error.Unsupportedifscan't write, the encoder's error, ornil
extract_current_to_dynamic ¶
extract_current_to_dynamic :: proc( it: ^Iterator, output: ^[dynamic]u8, ) -> (err: archive.Error) {…}
Reads the current member's data into a buffer; a sparse file's holes are filled with zeros
The data is read once: what this or open_current's reader has read is gone.
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
Returns:
- err
General_Error.No_Current_Memberifiteratehasn't returned a member, an allocator error, the member's error, ornil
extract_current_to_writer ¶
Writes the current member's data to a writer; a sparse file's holes are written as zeros
The data is read once: what this or open_current's reader has read is gone.
Inputs:
- it The iterator, on the member
iteratereturned last - w Where the member's data is written
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, ) -> (err: archive.Error) {…}
Reads a member's data into a buffer, moving the reader's position to it
Going back to an earlier member rewinds the reader.
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
Returns:
- err
General_Error.Not_Foundfor a handle past the last member,General_Error.Not_Seekableif a stream needs a rewind it can't do, an allocator error, the member's error, ornil
extract_to_writer ¶
extract_to_writer :: proc( r: ^Reader, handle: archive.Handle, w: io.Stream, ) -> (err: archive.Error) {…}
Writes a member's data to a writer, moving the reader's position to it
Going back to an earlier member rewinds the reader.
Inputs:
- r An open reader
- handle The member, from
File_Info.handleorfind - w Where the member's data is written
Returns:
- err
General_Error.Not_Foundfor a handle past the last member,General_Error.Not_Seekableif a stream needs a rewind it can't do, 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, moving the reader's position to it
NOTE: The search starts at the current member and goes forward, then rewinds and goes on up to where it started. Of several members with the path, it finds the next one. Searching forward first saves decompressing a compressed archive again from the start, and works on a pipe.
Inputs:
- r An open reader
- path The member's path, ignoring a leading
./or trailing/
Returns:
- fi The member's info; its strings last as long as those from
iterate - handle The member, for
extractandopen_member - ok
falseif no member has the path - err An error reading the archive,
General_Error.Not_Seekableif a stream needs a rewind it can't do, ornil
info ¶
info :: proc(r: ^Reader) -> (info: archive.Archive_Info) {…}
Summarizes an archive
Inputs:
- r An open reader
Returns:
- info Its format, compression, and volume label as the comment;
member_countis -1 until a read has reached the end
iterate ¶
Moves to the next member
Inputs:
- it The iterator
Returns:
- fi The member's info; its strings last until the reader moves to another member, so keep them
with
file_info_clone - index The member's index, as in
fi.handle - ok
falseafter the last member or on an error;iterator_errortells which
iterator_create ¶
Creates an iterator over an archive's members, in storage order
NOTE: The reader has one position, which its iterators, find, extract and open_member
share; creating an iterator rewinds it.
Inputs:
- r An open reader
Returns:
- it The iterator; destroy it with
iterator_destroy. If the rewind fails, as on a stream that can't seek,iteratereturnsfalseanditerator_errortells why
iterator_destroy ¶
iterator_destroy :: proc(it: ^Iterator) {…}
Destroys an iterator
Inputs:
- it The iterator
iterator_error ¶
Tells why iteration stopped
Inputs:
- it The iterator
Returns:
- path The path of the member the error is about, if any
- err The error that stopped iteration,
nilif it reached the end
next_record ¶
Splits the first pax record off a block of them, such as Entry.local_records
Inputs:
- records The records; advanced past the first one when
ok
Returns:
- key The record's key, a slice of
records - value The record's value, a slice of
records - ok
falseat the end or on a malformed record
open_current ¶
Opens the current member's data as a reader, which decompresses as it is read
The data is read once: what this reader or extract_current has read is gone.
Inputs:
- it The iterator, on the member
iteratereturned last
Returns:
- reader The member's reader; it reads at the reader's position and lasts until that moves to another member
- err
General_Error.No_Current_Memberifiteratehasn't returned a member, or the member's error, ornil
open_from_bytes ¶
open_from_bytes :: proc( r: ^Reader, data: []u8, options: compress.Options = compress.Options{}, allocator := context.allocator, ) -> (err: archive.Error) {…}
Opens a tar archive held in memory, plain or compressed with gzip, bzip2, LZ4, LZMA or XZ
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
General_Error.Unknown_Formatifdatastarts with neither a tar header nor a compressed stream, the decoder's error, 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 tar archive file, plain or compressed with gzip, bzip2, LZ4, LZMA or XZ
Unlike zip's and 7z's, the file isn't locked.
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 An OS error, an error as from
open_from_stream, 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 tar archive from a stream, plain or compressed with gzip, bzip2, LZ4, LZMA or XZ
Allocates Using Provided Allocator
Inputs:
- r The reader;
closeit when done, it is left closed on an error - s The archive; it must read, and stay open until
close, which leaves it open. If it can't seek, the members can be read only once, in order - options Limits and progress callback for the decoders, see
compress.Options(default: {}) - allocator (default: context.allocator)
Returns:
- err An error reading
s,General_Error.Unknown_Formatifsstarts with neither a tar header nor a compressed stream, the decoder's error, ornil
open_member ¶
open_member :: proc( r: ^Reader, handle: archive.Handle, ) -> (reader: io.Stream, err: archive.Error) {…}
Opens a member's data as a reader, moving the reader's position to it
Going back to an earlier member rewinds the reader.
Inputs:
- r An open reader
- handle The member, from
File_Info.handleorfind
Returns:
- reader The member's reader; it reads at the reader's position and lasts until that moves to
another member. It needs no
close_member - err
General_Error.Not_Foundfor a handle past the last member,General_Error.Not_Seekableif a stream needs a rewind it can't do, the member's error, ornil
read_link ¶
read_link :: proc( it: ^Iterator, allocator := context.allocator, ) -> (target: string, err: archive.Error) {…}
Reads the current member's link target, which tar stores in the header
Allocates Using Provided Allocator
Inputs:
- it The iterator, on the member
iteratereturned last - allocator (default: context.allocator)
Returns:
- target The target stored with the member: a symlink's or a hard link's;
deleteit - err
General_Error.No_Current_Memberifiteratehasn't returned a member, an allocator error, ornil
write_directory ¶
Adds a directory tree: its directories, regular files and symlinks
Other file types are skipped. With deterministic, members go in sorted order.
Inputs:
- tw An open writer
- dir The directory to add
- prefix Prepended to every member path; when set,
diritself is added, namedprefix(default: "")
Returns:
- err The first error, from the walk or from
write_entry_with_info, ornil
write_entry_simple ¶
write_entry_simple :: proc( tw: ^Writer, name: string, data: Entry_Data = nil, ) -> (err: archive.Error) {…}
Adds a regular file, stamped with the current time
Inputs:
- tw An open writer
- name The member's path
- data The file's data, as bytes or a reader (default: nil)
Returns:
- err As
write_entry_with_info
write_entry_with_info ¶
write_entry_with_info :: proc( tw: ^Writer, fi: archive.File_Info, data: Entry_Data = nil, ) -> (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. A reader
of unknown size is read into memory first, within options.max_memory.
Inputs:
- tw An open writer
- fi The member's path, type and metadata; any type but
.Anti.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)
Returns:
- err
General_Error.Unsupported_Entry,General_Error.Incompatible_Optionsfor data on a non-file, a link without a target or a field the format can't hold,Tar_Error.Size_Mismatchif the reader doesn't givefi.sizebytes, ornil
Procedure Groups
create ¶
create :: proc{ create_from_path, create_from_stream, }
Starts a new tar 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 tar archive from a path, a byte slice or a stream
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