archive

package
v1.102.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2024 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Overview

Package archive contains methods for extracting file(s) from arbitrary archive types. The archive types supported are:

  • tar
  • tar.gz
  • tar.xz
  • tar.bz2
  • zip

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Archive

type Archive interface {
	// Next returns the next file in the archive, or returns
	// io.EOF if there are no more files.
	Next() (*Header, io.ReadCloser, error)
}

Archive is an interface for interacting with files inside of an archive

type CompressedReader

type CompressedReader interface {
	// Open returns a reader for the compressed file.
	// This only returns a io.ReadCloser because it should only contain a single file
	Open(ctx context.Context, r io.Reader) (io.ReadCloser, error)
}

CompressedReader is the interface for a reader that can read a compressed file.

type ExtractOptionFunc

type ExtractOptionFunc func(*ExtractOptions) error

ExtractOptionFunc is an option function that mutates an ExtractOptions struct.

func WithFilePath

func WithFilePath(filePath string) ExtractOptionFunc

WithFilePath is an ExtractOptionFunc that sets the file path to extract out of the provided archive.

func WithFilePathSelector

func WithFilePathSelector(fn func(string) bool) ExtractOptionFunc

WithFilePathSelector is an ExtractOptionFunc that sets the file path selector function. See ExtractOptions.FilePathSelectorFunc for more details.

type ExtractOptions

type ExtractOptions struct {
	// FilePath is the file path to extract out of the provided archive
	FilePath string

	// FilePathSelectorFunc is a function that can be used to select a file path
	// during extraction when there's a set criteria (or logic) that needs to be
	// ran to determine which file path to extract out of the archive.
	//
	// Note: When this function returns true, the file is returned. Multiple files
	// are not supported at this time.
	FilePathSelectorFunc func(string) bool
}

ExtractOptions are the options for the Extract function.

type Extractor

type Extractor interface {
	// Open returns a reader for the archive file
	Open(ctx context.Context, archiveName string, archive io.Reader) (Archive, error)

	// Close closes the reader returned by Open
	Close() error
}

Extractor is the interface for creating a io.ReadCloser from an archive.

type Header struct {
	// Name is the name of the file entry
	Name string

	// Mode is the mode of the file entry
	Mode int64

	// Size is the size of the file entry
	Size int64

	// Type is the type of entry this is
	// (file, directory, etc)
	Type HeaderType
}

Header is a generic struct containing information about a file in an archive.

func Extract

func Extract(ctx context.Context, archiveName string, r io.Reader,
	optFns ...ExtractOptionFunc) (io.ReadCloser, *Header, error)

Extract extracts a file, based on the provided option functions, from the provided archive.

type HeaderType

type HeaderType string

HeaderType is the type of entry a Header is for in an archive

const (
	// HeaderTypeFile is a file entry
	HeaderTypeFile HeaderType = "file"

	// HeaderTypeDirectory is a directory entry
	HeaderTypeDirectory HeaderType = "directory"
)

Jump to

Keyboard shortcuts

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