POSIX ustar tar archive header encoding and decoding.
Round-trip a regular-file header
Round-trip a regular-file header
import { assertEquals } from "@std/assert"; import { TAR_BLOCK_SIZE, TAR_TYPEFLAG, USTAR_MAGIC, USTAR_VERSION, ustarHeader, } from "@binstruct/tar"; const coder = ustarHeader(); const header = { name: "hello.txt", mode: 0o644, uid: 1000, gid: 1000, size: 5, mtime: 1_700_000_000, checksum: 0, typeflag: TAR_TYPEFLAG.regularFile, linkname: "", magic: USTAR_MAGIC, version: USTAR_VERSION, uname: "user", gname: "user", devmajor: "", devminor: "", prefix: "", padding: new Uint8Array(12), }; const buffer = new Uint8Array(TAR_BLOCK_SIZE); const written = coder.encode(header, buffer); const [decoded, read] = coder.decode(buffer); assertEquals(written, TAR_BLOCK_SIZE); assertEquals(read, TAR_BLOCK_SIZE); assertEquals(decoded, header);
Creates a coder for a single 512-byte POSIX ustar header block.
Decoded ustar header — one 512-byte block describing a single archive member.
Size in bytes of a single tar block, and therefore of the ustar header itself. Every header and every member's data are padded to a multiple of this size.
Well-known values of the ustar typeflag field. Only the subset relevant
to a header-only, non-archival v0.0.1 is included.
-
directory: string
A directory.
-
hardLink: string
A hard link to another member already in the archive.
-
regularFile: string
A regular file.
-
symlink: string
A symbolic link;
linknameholds the link target.
The ustar magic field: the literal string "ustar" stored NUL-terminated
in a 6-byte field. Distinguishes a ustar header from the older, incompatible
V7 tar format.
The ustar version field for the format this coder targets: the two ASCII
digits "00" (not NUL-terminated — it fills the whole 2-byte field).