default

POSIX ustar tar archive header encoding and decoding.

Examples

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);

Functions

f
ustarHeader(): Coder<UstarHeader>

Creates a coder for a single 512-byte POSIX ustar header block.

Interfaces

Variables

v
TAR_BLOCK_SIZE: 512

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.

v
TAR_TYPEFLAG: { regularFile: string; hardLink: string; symlink: string; directory: string; }

Well-known values of the ustar typeflag field. Only the subset relevant to a header-only, non-archival v0.0.1 is included.

v
USTAR_MAGIC: "ustar"

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.

v
USTAR_VERSION: "00"

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).