Documentation
¶
Overview ¶
Package archives provides recursive walking of archive and compressed-container files (zip, tar, gzip, bzip2, xz, zstd, ar/deb), yielding each contained file as a stream. It is built for tools that need to read inside nested containers — firmware images, Android APKs, Debian packages, JARs — such as scanners, indexers, and forensics utilities.
The entry point is Walk. Walking is bounded against decompression bombs and pathological nesting via Options (maximum depth and maximum total decompressed bytes). The package depends only on the standard library plus pure-Go xz and zstd decoders, so it adds no CGO.
Index ¶
Constants ¶
const ( DefaultMaxDepth = 8 // DefaultMaxBytes caps total decompressed bytes processed per top-level // input (2 GiB), a backstop against decompression bombs. DefaultMaxBytes int64 = 2 << 30 )
Default limits guarding against decompression bombs and pathological nesting.
const PathSeparator = "!/"
PathSeparator joins an archive path to a member path in virtual paths, e.g. "firmware.tar.gz!/bin/busybox". It mirrors the convention used by jar URLs.
Variables ¶
This section is empty.
Functions ¶
func IsArchiveName ¶
IsArchiveName reports whether a filename looks like a supported archive by its extension. Used to decide, before opening, whether recursion is applicable.
func Walk ¶
Walk opens the file at path, detects its container format, and invokes fn for every leaf file in the (possibly nested) archive tree. A non-archive file is emitted as a single leaf at its own path. Decompression-bomb and depth limits are enforced per Options.
The walk is cancellable via ctx: cancellation is observed between members and mid-stream during long reads, and Walk returns ctx.Err() (context.Canceled or context.DeadlineExceeded) when the walk is aborted. A graceful budget-exceeded stop is reported as a nil error.
Types ¶
type Format ¶
type Format int
Format identifies a container/compression format.
const ( FormatNone Format = iota // not a recognized archive (leaf file) FormatZip // zip and zip-based (apk/jar/ipa/...) FormatTar // uncompressed tar FormatGzip // gzip stream FormatBzip2 // bzip2 stream FormatXz // xz stream FormatZstd // zstandard stream FormatAr // Unix ar archive (Debian .deb) )
Supported formats.
type Options ¶
type Options struct {
// MaxDepth bounds archive nesting; members deeper than this are emitted as
// opaque leaves rather than descended into. 0 uses DefaultMaxDepth.
MaxDepth int
// MaxBytes bounds total decompressed bytes per top-level input. 0 uses
// DefaultMaxBytes; negative means unlimited.
MaxBytes int64
// Logger receives non-fatal warnings encountered during a walk — an
// unreadable member, a malformed sub-archive, the byte budget being hit.
// These conditions are recovered (the walk skips the member, scans the raw
// bytes, or stops gracefully) rather than returned as errors, so they are
// reported here. A nil Logger (the default) discards them, keeping the
// library silent unless the caller opts in.
Logger *slog.Logger
}
Options configures a Walk.
type WalkFunc ¶
WalkFunc is invoked once per leaf file discovered in the archive tree. ctx is the context passed to Walk, allowing the callback to honor cancellation in its own work. path is the virtual path (e.g. "pkg.deb!/data.tar!/usr/bin/htop") and r streams the member's decompressed contents. Returning an error aborts the walk.