# gizlang/archive

tar (POSIX ustar with pax records, GNU long names) and zip archives, read and written. The compression of their members is gizlang/compress.

```gizlang
import * as Archive from "gizlang/archive";
```

## Types

### `TarMember`

```gizlang
type TarMember = { path: string; kind: string; mode: i64; data: bytes; link: string; uid: i64; gid: i64; modified: i64; major: i64; minor: i64; attributes: Vec<TarAttribute>; }
```

One archive member. `kind` is "file", "directory", "symlink", "hardlink", "character", "block", "fifo" or "other";
`link` is the target of a link; `major` and `minor` name a device. The owner, the time (seconds) and the extended
attributes (pax SCHILY.xattr records) are what the archive says: a root file system needs them as written.

- `path`: `string`
- `kind`: `string`
- `mode`: `i64`
- `data`: `bytes`
- `link`: `string`
- `uid`: `i64`
- `gid`: `i64`
- `modified`: `i64`
- `major`: `i64`
- `minor`: `i64`
- `attributes`: `Vec<gizlang_archive.TarAttribute>`

### `TarAttribute`

```gizlang
type TarAttribute = { name: string; value: bytes; }
```

An extended attribute of a member: its full name ("security.capability") and value.

- `name`: `string`
- `value`: `bytes`

### `ZipWriter`

```gizlang
type ZipWriter
```

A zip archive, written in order: the caller writes what each call returns. An entry comes whole (`file`: its bytes
stored or deflated, its CRC and sizes in its header) or as it streams (`entry`, then `data` for each piece of its
bytes, which the caller writes as they are, then a data descriptor). A streamed entry is stored: most bytes people
keep are compressed already, and storing keeps the archive at disk speed. Names are UTF-8 (flag bit 11 when a name is
not ASCII). A modified time goes as a DOS time and as UTC seconds (extra 0x5455); modified 0 writes no time
(1980-01-01 and no extra field), as the first entry of an OCF container needs. ZIP64 records come in only where a
size, an offset or the count needs them. `finish` writes the central directory.

Opaque: create and use it through this module's functions.

### `ZipEntry`

```gizlang
type ZipEntry = { name: string; size: i64; stored: i64; method: u64; crc: u64; modified: i64; offset: i64; }
```

One entry of a zip archive, as its central directory records it: NAME (a folder's ends in "/"), SIZE bytes, STORED
of them in the archive by METHOD (0 stored, 8 deflated), their CRC, MODIFIED (ms since 1970: extra 0x5455's UTC
seconds, else the DOS time read as UTC, 0 for none) and OFFSET, where its local header is.

- `name`: `string`
- `size`: `i64`
- `stored`: `i64`
- `method`: `u64`
- `crc`: `u64`
- `modified`: `i64`
- `offset`: `i64`

### `ZipFile`

```gizlang
type ZipFile = { entries: Vec<ZipEntry>; member: (e: ZipEntry, limit: i64) => bytes throws string; extract: (e: ZipEntry, out: string) => void throws string; }
```

A zip archive in a file, read a part at a time (a package of gigabytes is never read whole): its first MOST
ENTRIES, each one's bytes (MEMBER, at most LIMIT), and each one written to a file in pieces (EXTRACT: OUT appears
whole, checked against its CRC, or not at all).

- `entries`: `Vec<gizlang_archive.ZipEntry>`
- `member`: `(arg0: gizlang_archive.ZipEntry, arg1: i64) => bytes throws string`
- `extract`: `(arg0: gizlang_archive.ZipEntry, arg1: string) => void throws string`

## Functions

### `untar`

```gizlang
function untar(archive: bytes, limit: i64): Vec<TarMember> throws DecodeError
```

Every member of `archive` in order, with pax and GNU long-name headers applied to the member after them. It reads the
archive as given: the caller decides which members to keep. It fails (CORRUPT) on a header whose checksum is wrong or
whose data runs past the archive, and (TOO_LARGE) when the members hold more than `limit` bytes (their bodies and
the pax and long-name records).

Effects: fail, alloc.

### `tarPadding`

```gizlang
function tarPadding(size: i64): i64
```

The zero bytes after a member body of `size` bytes, to the next 512-byte block.

Effects: fail.

### `tarHeader`

```gizlang
function tarHeader(path: string, mode: i64, size: i64, link: string): bytes
```

The header blocks of one member fixed by its fields alone (owner 0, time 0): a pax header when PATH or LINK is longer
than 100 bytes, then the ustar block. A member with a LINK is a hard link with no body; otherwise it is a file of SIZE
bytes, whose body and `tarPadding(size)` zero bytes follow. Two archives of the same members are the same bytes.

Effects: fail, alloc.

### `newZipWriter`

```gizlang
function newZipWriter(): ZipWriter
```

A writer of a zip archive in memory, with no entries.

### `zipEntries`

```gizlang
function zipEntries(size: i64, read: (at: i64, count: i64) => bytes throws string, most: i64, names: string = ""): Vec<ZipEntry> throws DecodeError
```

The first MOST entries of the zip archive of SIZE bytes that READ(at, count) reads, in their central directory's
order, ZIP64 included. It reads the end records and the central directory, never an entry's bytes. A name that is not
UTF-8 (an old archive's code page) is read in the encoding NAMES (gizlang/encoding's label, as "euc-kr" for an archive
made on Korean Windows); with none, each byte past ASCII shows as U+FFFD.

Effects: fail, read, write, console, io, alloc.

### `zipFile`

```gizlang
function zipFile(path: string, most: i64 = 100000): ZipFile throws string
```

The zip archive in the file at PATH (zipEntries, unzipEntry over its file).

Effects: fail, read, write, console, io, alloc.

### `unzipEntry`

```gizlang
function unzipEntry(e: ZipEntry, read: (at: i64, count: i64) => bytes throws string, limit: i64): bytes throws DecodeError
```

The bytes of entry E of the archive that READ(at, count) reads, checked against its CRC: TOO_LARGE past LIMIT bytes,
UNSUPPORTED when it is encrypted or packed by a method other than stored or deflated.

Effects: fail, read, write, console, io, alloc.

## Methods

### `ZipWriter.entry`

```gizlang
function ZipWriter.entry(self: mut ZipWriter, name: string, modified: i64): bytes
```

The local header of a streamed entry NAME (a folder ends in "/"), modified at MODIFIED (ms since 1970, 0: no time);
it ends the streamed entry before.

Effects: fail, alloc.

### `ZipWriter.data`

```gizlang
function ZipWriter.data(self: mut ZipWriter, data: bytes): void
```

Counts DATA, the next bytes of the streamed entry, which the caller writes as they are.

Effects: fail.

### `ZipWriter.file`

```gizlang
function ZipWriter.file(self: mut ZipWriter, name: string, modified: i64, data: bytes, deflated: bool): bytes
```

Entry NAME whole: its header and DATA, deflated when DEFLATED and that is smaller, modified at MODIFIED (ms since
1970, 0: no time). It ends the streamed entry before.

Effects: fail, alloc.

### `ZipWriter.finish`

```gizlang
function ZipWriter.finish(self: mut ZipWriter): bytes
```

The end of the archive: a streamed entry's descriptor, the central directory and the end records.

Effects: fail, alloc.

