# Compress
`compress/` packs and unpacks archive data entirely in memory: tar,
gzip, and tar.gz helpers that consume and produce byte strings. They are
native stdlib, available in every host without an import. Combine them
with Filesystem to move archives to and from disk, and
with Crypto when a payload needs hashing or encryption before
it ships.
## Functions
| Function | Purpose |
|---|---|
| `compress/tar-create $dir-path` | Pack a directory tree into a USTAR tar; returns archive bytes |
| `compress/tar-extract $bytes $dest-dir` | Extract tar bytes into `$dest-dir` (created if missing) |
| `compress/gzip $bytes` | Gzip-compress bytes into an RFC 1952 container |
| `compress/gunzip $bytes` | Decompress gzip-encoded bytes back to the original |
| `compress/tgz-create $dir-path` | Pack a directory tree into a tar.gz; gzip over `tar-create` |
| `compress/tgz-extract $bytes $dest-dir` | Decompress then untar into `$dest-dir` |
| `compress/list-tgz $bytes` | Vector of entry paths inside a tar.gz, without extracting |
All arguments and results are binary-safe strings:
- `tar-create` and `tgz-create` take a directory path (relative or
absolute). Entry paths inside the archive are relative to the
directory's basename, so `[compress/tar-create "./src"]` stores
`src/...` entries — extraction recreates that layout under
`$dest-dir`.
- `tar-extract` and `tgz-extract` return `true` on success and create
`$dest-dir` when it is missing. Regular files, directories, and
symlinks are restored.
- `list-tgz` returns a vector of path strings (`@{ "src/a.dft" ... }`).
- The gzip encoder writes stored deflate blocks: the output is valid
gzip that any decoder reads, and it is cheap to produce, though it
stores data rather than compressing it.
- Every function returns `%Err{ :message "..." }` on malformed input
instead of throwing; check with `err?` before using the result.
## Examples
# Pack a tree, write it to disk, list and restore it set tgz [compress/tgz-create "./src"] [fs/write "src.tgz" $tgz] [compress/list-tgz $tgz] # => @{ "src/main.dft" "src/util.dft" ... } set ok [compress/tgz-extract [unwrap [fs/read "src.tgz"]] "./backup"] # => true # The tar and gzip layers work on their own set tar [compress/tar-create "./src"] set copy [compress/gunzip [compress/gzip $tar]] # Errors come back as values, so check before use set bad [compress/gunzip "not a gzip stream"] if [err? $bad] { echo "bad archive:" $bad~message }
Two archives of the same directory are independent byte strings, so
`tgz-create` output can be stored, shipped, or hashed like any other
value (see Crypto).
## See Also
- Filesystem — read and write archive byte strings
- Crypto — hashing, HMAC, and symmetric crypto
- Data Formats — json and yaml counterparts