archive

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package archive lists and extracts single files from release archives.

It handles zip files, tarballs (plain or compressed with gzip, bzip2, xz or zstd) and bare compressed files, detecting the format from the data and falling back to the filename extension. Extraction is deliberately limited to one regular file at a time, written to a path chosen by the caller and capped in size, so the installer never trusts paths or sizes stored in an archive.

Index

Constants

View Source
const (
	ContainerTar = "tar"
	ContainerZip = "zip"
)

Container formats

View Source
const (
	CompressionGzip  = "gzip"
	CompressionBzip2 = "bzip2"
	CompressionXz    = "xz"
	CompressionZstd  = "zstd"
)

Compression formats

Variables

View Source
var (
	ErrUnknownFormat  = errors.New("unrecognized archive format")
	ErrEntryNotFound  = errors.New("entry not found in archive")
	ErrNotRegularFile = errors.New("archive entry is not a regular file")
	ErrTooLarge       = errors.New("archive entry exceeds the maximum extraction size")
)

Functions

func Extract

func Extract(archivePath, entryPath, dst string, maxSize int64) error

Extract writes the regular file stored at entryPath in the archive to dst. The output is truncated if it exists and created with mode 0600. At most maxSize bytes are written: larger entries fail with ErrTooLarge and the partial output is removed. A maxSize of zero or less disables the cap.

func List

func List(archivePath string) ([]Entry, Format, error)

List returns the entries stored in the archive at path along with the detected format. Only regular files, directories and symlinks are listed; hard links, devices and metadata headers are skipped, as are entries with unsafe names. When the same name appears more than once, the first occurrence wins.

Types

type Entry

type Entry struct {
	// Path is the slash separated, cleaned path of the entry inside the
	// archive. It is always relative and never escapes the archive root.
	Path string

	// Mode carries the entry type (regular file, directory or symlink)
	// and the permission bits recorded in the archive.
	Mode fs.FileMode

	// Linkname is the target of symbolic link entries, as stored.
	Linkname string

	// Head holds the leading bytes of regular files for content sniffing.
	// It is shorter than headSize when the file is.
	Head []byte
}

Entry describes a file stored in an archive.

func (*Entry) Base

func (e *Entry) Base() string

Base returns the last element of the entry path.

func (*Entry) IsDir

func (e *Entry) IsDir() bool

IsDir returns true if the entry is a directory.

func (*Entry) IsRegular

func (e *Entry) IsRegular() bool

IsRegular returns true if the entry is a regular file.

func (e *Entry) IsSymlink() bool

IsSymlink returns true if the entry is a symbolic link.

type Format

type Format struct {
	Container   string
	Compression string
}

Format describes how an archive is put together: the container holding the entries and the compression applied to it. A bare compressed file (a gzipped binary, for example) has a compression but no container.

func (Format) String

func (f Format) String() string

String returns a label for the format such as "tar+gzip", "zip" or "xz".

Jump to

Keyboard shortcuts

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