Help us shape the future of Go! Take the 2026 Go Developer Survey

archive

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package archive reads zip, tar and tar.gz archives: the walker the item service explodes and serves archive entries with, and a bounded walker and reader the format handlers (decision 0032) read packages with, such as an npm tarball's package.json and file count, a Go module zip's go.mod or a jar's POM. Every count an archive's headers give is the uploader's claim, so the bounded walker counts what it actually decompresses.

Index

Constants

View Source
const SniffLen = 512

SniffLen is how much of the content Sniff reads: a tar header's magic ends at byte 262.

Variables

View Source
var DefaultLimits = Limits{Entries: 1000, Inflated: 256 << 20, File: 1 << 20}

DefaultLimits suit the descriptors formats read: a package.json, a go.mod or a POM is kilobytes, and a package whose descriptor lies past a thousand entries or 256 MiB of content is refused rather than read.

View Source
var ErrNotFound = errors.New("archive: no such entry")

ErrNotFound is an archive with no entry the caller asked for.

View Source
var ErrUnusable = errors.New("archive: unusable archive")

ErrUnusable is content that is not a usable archive: an unknown or corrupt format, an entry escaping its folder, or a limit exceeded.

Functions

func Clean

func Clean(name string) (string, error)

Clean makes an entry name a relative path, refusing names that would leave the folder the archive is read into: absolute paths, drive letters and ".." segments. Backslashes count as separators, as Windows tools write them. A name that is only the folder itself cleans to "".

func ReadEntry

func ReadEntry(e Entry, lim Limits) ([]byte, error)

ReadEntry reads e whole, refusing content past lim.File with ErrUnusable.

func ReadFile

func ReadFile(
	ra io.ReaderAt,
	size int64,
	kind Kind,
	lim Limits,
	want func(name string) bool,
) (string, []byte, error)

ReadFile returns the first regular file of the archive in ra whose cleaned name want accepts, with that name. ErrNotFound when no file matches; ErrUnusable when the content is no archive of kind or a limit is passed first.

func ReaderAt

func ReaderAt(rs io.ReadSeeker) io.ReaderAt

ReaderAt gives random access over a reader that can only seek, such as stored content, for zip, which reads its central directory from the end. Reads are serialised because they share one seek position. A reader that already reads at offsets is returned as it is.

func Scan

func Scan(ra io.ReaderAt, size int64, kind Kind, lim Limits, fn func(Entry) error) error

Scan is Walk within lim's Entries and Inflated: ErrUnusable once the walk passes either, so a format can read a whole package, such as an npm tarball it counts the files of, without trusting its headers. lim.File is ReadEntry's.

func Walk

func Walk(ra io.ReaderAt, size int64, kind Kind, fn func(Entry) error) error

Walk calls fn for every entry of the archive in ra, in archive order, until fn returns an error, which Walk returns.

Types

type Entry

type Entry struct {
	// Name is the entry's name as the archive spells it; Clean makes it a
	// path.
	Name string
	Dir  bool
	// Regular is a plain file; links and devices are not.
	Regular bool
	// Size is what the archive's header claims.
	Size int64
	// Open reads the entry. A tar entry's reader is valid only until the
	// walk moves on.
	Open func() (io.Reader, error)
}

Entry is one entry as an archive lists it.

type Kind

type Kind int

Kind is an archive container format.

const (
	None Kind = iota
	Zip
	Tar
	TarGz
)

Archive kinds; None is a file none of them.

func KindOf

func KindOf(ra io.ReaderAt, name string) Kind

KindOf sniffs the kind of the content in ra, named name.

func ParseKind

func ParseKind(s string) (Kind, bool)

ParseKind reads an archiveType parameter (zip, tar, tar.gz, tgz).

func Sniff

func Sniff(head []byte, name string) Kind

Sniff tells the kind from the content's first bytes, falling back to the name for what has no signature to read.

func (Kind) ContentType

func (k Kind) ContentType() string

ContentType is the media type of an archive of the kind.

func (Kind) Ext

func (k Kind) Ext() string

Ext is the file extension of an archive of the kind.

type Limits

type Limits struct {
	// Entries is how many entries the walk may pass.
	Entries int
	// Inflated bounds the bytes a tar.gz walk decompresses, including the
	// entries it skips on the way, which a zip walk does not read.
	Inflated int64
	// File bounds the content of the file read.
	File int64
}

Limits bound what reading one file from an archive may cost.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL