package core:archive/tar

⌘K
Ctrl+K
or
/

    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.

    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 ¶

    Entry_Data :: union {
    	[]u8, 
    	io.Stream, 
    }
     

    A regular file's data, for write_entry

    Related Procedures With Parameters

    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

    Iterator ¶

    Iterator :: struct {
    	reader:   ^Reader,
    	err:      archive.Error,
    	err_path: [dynamic]u8,
    }
     

    Walks a Reader's members, from iterator_create

    Related Procedures With Parameters

    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

    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

    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

    Constants

    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:

    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:

    commit ¶

    commit :: proc(tw: ^Writer) -> (err: archive.Error) {…}
     

    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; close it 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.Busy if another process holds the archive, an OS error, or nil

    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; close it 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_Options or General_Error.Unsupported_Method for bad options, io.Error.Unsupported if s can't write, the encoder's error, or nil

    current_entry ¶

    current_entry :: proc(it: ^Iterator) -> (entry: ^Entry) {…}
     

    Returns the current member's raw header, for what File_Info leaves out

    Inputs:

    • it The iterator, on the member iterate returned last

    Returns:

    • entry The member's header block, with its pax records; nil if iterate hasn't returned a member. It lasts until the reader moves to another member

    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 iterate returned last
    • output Replaced by the member's data, grown with its own allocator; emptied on an error

    Returns:

    • err General_Error.No_Current_Member if iterate hasn't returned a member, an allocator error, the member's error, or nil

    extract_current_to_writer ¶

    extract_current_to_writer :: proc(it: ^Iterator, w: io.Stream) -> (err: archive.Error) {…}
     

    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 iterate returned last
    • w Where the member's data is written

    Returns:

    • err General_Error.No_Current_Member if iterate hasn't returned a member, or the member's error, or nil

    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.handle or find
    • output Replaced by the member's data, grown with its own allocator; emptied on an error

    Returns:

    • err General_Error.Not_Found for a handle past the last member, General_Error.Not_Seekable if a stream needs a rewind it can't do, an allocator error, the member's error, or nil

    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.handle or find
    • w Where the member's data is written

    Returns:

    • err General_Error.Not_Found for a handle past the last member, General_Error.Not_Seekable if a stream needs a rewind it can't do, the member's error, or nil

    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 extract and open_member
    • ok false if no member has the path
    • err An error reading the archive, General_Error.Not_Seekable if a stream needs a rewind it can't do, or nil

    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_count is -1 until a read has reached the end

    iterate ¶

    iterate :: proc(it: ^Iterator) -> (fi: archive.File_Info, index: int, ok: bool) {…}
     

    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 false after the last member or on an error; iterator_error tells which

    iterator_create ¶

    iterator_create :: proc(r: ^Reader) -> (it: Iterator) {…}
     

    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:

    iterator_destroy ¶

    iterator_destroy :: proc(it: ^Iterator) {…}
     

    Destroys an iterator

    Inputs:

    • it The iterator

    iterator_error ¶

    @(require_results)
    iterator_error :: proc(it: ^Iterator) -> (path: string, err: archive.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, nil if it reached the end

    next_record ¶

    next_record :: proc(records: ^[]u8) -> (key, value: string, ok: bool) {…}
     

    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 false at the end or on a malformed record

    open_current ¶

    open_current :: proc(it: ^Iterator) -> (reader: io.Stream, err: archive.Error) {…}
     

    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 iterate returned 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_Member if iterate hasn't returned a member, or the member's error, or nil

    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; close it 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_Format if data starts with neither a tar header nor a compressed stream, the decoder's error, or nil

    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; close it 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:

    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; close it 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_Format if s starts with neither a tar header nor a compressed stream, the decoder's error, or nil

    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.handle or find

    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_Found for a handle past the last member, General_Error.Not_Seekable if a stream needs a rewind it can't do, the member's error, or nil
    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 iterate returned last
    • allocator (default: context.allocator)

    Returns:

    • target The target stored with the member: a symlink's or a hard link's; delete it
    • err General_Error.No_Current_Member if iterate hasn't returned a member, an allocator error, or nil

    write_directory ¶

    write_directory :: proc(tw: ^Writer, dir: string, prefix: string = "") -> (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:

    • tw An open writer
    • dir The directory to add
    • prefix Prepended to every member path; when set, dir itself is added, named prefix (default: "")

    Returns:

    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:

    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. size may be -1 for a reader of unknown size
    • data A regular file's data, as bytes or a reader; nil for other types (default: nil)

    Returns:

    • err General_Error.Unsupported_Entry, General_Error.Incompatible_Options for data on a non-file, a link without a target or a field the format can't hold, Tar_Error.Size_Mismatch if the reader doesn't give fi.size bytes, or nil

    Procedure Groups

    close ¶

    close :: proc{
    	close_reader,
    	close_writer,
    }
    
     

    Closes a reader or a writer

    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 ¶

     

    Extracts the current member's data to a writer or into a buffer

    open ¶

     

    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