packkit
Compress and decompress bytes, read and write archives, and detect a format from a file name or the first bytes of a file.
import gleam/option.{None}
import packkit
import packkit/archive.{Archive, File, Tar}
import packkit/codec.{Gzip}
let backup =
Archive(
format: Tar,
entries: [archive.new_entry(path: "hello.txt", kind: File(<<"hi":utf8>>))],
comment: None,
)
let recipe = packkit.Recipe(format: Tar, codecs: [Gzip])
let assert Ok(bytes) = packkit.pack(backup, using: recipe)
let assert Ok(restored) = packkit.unpack(bytes, using: recipe)
Types
What detect_filename, detect_bytes and detect found.
pub type Detected {
Compressed(codec: codec.Codec)
Archived(recipe: Recipe)
}
Constructors
-
Compressed(codec: codec.Codec)A single compressed stream, such as
.gzor.zst. -
Archived(recipe: Recipe)An archive, possibly compressed, such as
.tar.gzor.zip.
An archive format and the codecs wrapped around it, listed from the
inside out. Recipe(format: Tar, codecs: [Gzip]) is a .tar.gz.
pub type Recipe {
Recipe(
format: archive.ArchiveFormat,
codecs: List(codec.Codec),
)
}
Constructors
-
Recipe(format: archive.ArchiveFormat, codecs: List(codec.Codec))
Values
pub fn compress(
bytes: BitArray,
with codec: codec.Codec,
) -> Result(BitArray, codec.CodecError)
Compresses bytes with codec.
Examples
let assert Ok(compressed) = compress(<<"hello":utf8>>, with: Gzip)
decompress(compressed, with: Gzip)
// -> Ok(<<"hello":utf8>>)
pub fn decompress(
bytes: BitArray,
with codec: codec.Codec,
) -> Result(BitArray, codec.CodecError)
Decompresses bytes with codec under the default Limits.
Returns InvalidData when the bytes are not a valid stream and
LimitExceeded when the input or output is larger than the limits.
Examples
decompress(<<"not gzip":utf8>>, with: Gzip)
// -> Error(InvalidData("gzip header truncated"))
pub fn decompress_with_limits(
bytes: BitArray,
with codec: codec.Codec,
limits limits: limit.Limits,
) -> Result(BitArray, codec.CodecError)
Decompresses bytes with codec under limits.
pub fn detect(
path path: String,
bytes bytes: BitArray,
) -> Result(Detected, Nil)
Detects a format from the file name, and from the first bytes of the
file when the name says nothing, such as for - or an upload with a
random name.
pub fn detect_bytes(bytes: BitArray) -> Result(Detected, Nil)
Detects a format from the first bytes of a file. 512 bytes are enough for every format.
A gzip, xz or other compressed stream is reported as Compressed
even when it holds a tar archive, because the bytes do not say what
is inside. Raw DEFLATE and Brotli have no signature and are never
detected.
Examples
detect_bytes(<<0x1F, 0x8B, 0x08, 0x00>>)
// -> Ok(Compressed(Gzip))
detect_bytes(<<"plain text":utf8>>)
// -> Error(Nil)
pub fn detect_filename(path: String) -> Result(Detected, Nil)
Detects a format from the end of a file name, ignoring case. Compound
extensions win, so backup.tar.gz is a gzip-compressed tar archive
and not a plain gzip stream.
Examples
detect_filename("backup.tar.gz")
// -> Ok(Archived(Recipe(format: Tar, codecs: [Gzip])))
detect_filename("notes.txt")
// -> Error(Nil)
pub fn pack(
archive: archive.Archive,
using recipe: Recipe,
) -> Result(BitArray, archive.ArchiveError)
Writes archive and compresses it with the codecs of recipe, from
the inside out.
Returns FormatMismatch when the recipe’s format is not the
archive’s format, and CodecFailed when a codec fails.
pub fn read(
bytes: BitArray,
format format: archive.ArchiveFormat,
) -> Result(archive.Archive, archive.ArchiveError)
Reads an archive of format from bytes under the default Limits.
Entries whose path is absolute, contains .. or is otherwise unsafe
to extract are rejected with EntryRejected.
pub fn read_with_limits(
bytes: BitArray,
format format: archive.ArchiveFormat,
limits limits: limit.Limits,
) -> Result(archive.Archive, archive.ArchiveError)
Reads an archive of format from bytes under limits.
pub fn unpack(
bytes: BitArray,
using recipe: Recipe,
) -> Result(archive.Archive, archive.ArchiveError)
Decompresses bytes with the codecs of recipe, from the outside in,
and reads the archive under the default Limits.
pub fn unpack_with_limits(
bytes: BitArray,
using recipe: Recipe,
limits limits: limit.Limits,
) -> Result(archive.Archive, archive.ArchiveError)
Decompresses bytes with the codecs of recipe and reads the archive
under limits. Each codec and the archive reader get the same limits.
pub fn write(
archive: archive.Archive,
) -> Result(BitArray, archive.ArchiveError)
Writes archive in its own format. ZIP entries are stored without
compression and 7z uses LZMA; use packkit/zip or packkit/seven_z
to pick another method.
Returns EntryRejected for an entry whose path is empty, absolute,
contains ., .., an empty segment, a backslash or a NUL byte, and
for a negative mode, owner id or modification time. Returns
FieldOverflow when a value is too large for the format and
CommentUnsupported when the format has no place for the comment.
Examples
let unsafe = archive.new_entry(path: "../etc/passwd", kind: File(<<>>))
write(Archive(format: Tar, entries: [unsafe], comment: None))
// -> Error(EntryRejected(path: "../etc/passwd", reason: "path traversal"))