archiver

package module
v3.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2018 License: MIT Imports: 21 Imported by: 0

README

archiver archiver GoDoc Linux Build Status Windows Build Status

Introducing Archiver 3.0 - a cross-platform, multi-format archive utility and Go library. A powerful and flexible library meets an elegant CLI in this generic replacement for several of platform-specific, format-specific archive utilities.

Features

Package archiver makes it trivially easy to make and extract common archive formats such as zip and tarball (and its compressed variants). Simply name the input and output file(s). The arc command runs the same on all platforms and has no external dependencies (not even libc). It is powered by the Go standard library and several third-party, pure-Go libraries.

Files are put into the root of the archive; directories are recursively added, preserving structure.

  • Make whole archives from a list of files
  • Open whole archives to a folder
  • Extract specific files/folders from archives
  • Stream files in and out of archives without needing actual files on disk
  • Traverse archive contents without loading them
  • Compress files
  • Decompress files
  • Streaming compression and decompression
  • Several archive and compression formats supported
Format-dependent features
  • Optionally create a top-level folder to avoid littering a directory or archive root with files
  • Toggle overwrite existing files
  • Adjust compression level
  • Zip: store (not compress) already-compressed files
  • Make all necessary directories
  • Open password-protected RAR archives
  • Optionally continue with other files after an error
Supported archive formats
  • .zip
  • .tar
  • .tar.gz or .tgz
  • .tar.bz2 or .tbz2
  • .tar.xz or .txz
  • .tar.lz4 or .tlz4
  • .tar.sz or .tsz
  • .rar (open only)
Supported compression formats
  • bzip2
  • gzip
  • lz4
  • snappy
  • xz

Install

go get -u github.com/mholt/archiver/cmd/archiver

Or download binaries from the releases page.

Command Use

Make new archive
# Syntax: arc archive [archive name] [input files...]

$ arc archive test.tar.gz file1.txt images/file2.jpg folder/subfolder

(At least one input file is required.)

Extract entire archive
# Syntax: arc unarchive [archive name] [destination]

$ arc unarchive test.tar.gz

(The destination path is optional; default is current directory.)

The archive name must end with a supported file extension—this is how it knows what kind of archive to make. Run arc help for more help.

List archive contents
# Syntax: arc ls [archive name]

$ arc ls caddy_dist.tar.gz
drwxr-xr-x  matt    staff   0       2018-09-19 15:47:18 -0600 MDT   dist/
-rw-r--r--  matt    staff   6148    2017-08-07 18:34:22 -0600 MDT   dist/.DS_Store
-rw-r--r--  matt    staff   22481   2018-09-19 15:47:18 -0600 MDT   dist/CHANGES.txt
-rw-r--r--  matt    staff   17189   2018-09-19 15:47:18 -0600 MDT   dist/EULA.txt
-rw-r--r--  matt    staff   25261   2016-03-07 16:32:00 -0700 MST	dist/LICENSES.txt
-rw-r--r--  matt    staff   1017    2018-09-19 15:47:18 -0600 MDT   dist/README.txt
-rw-r--r--  matt    staff   288     2016-03-21 11:52:38 -0600 MDT   dist/gitcookie.sh.enc
...
Extract a specific file or folder from an archive
# Syntax: arc extract [archive name] [path in archive] [destination on disk]

$ arc extract test.tar.gz foo/hello.txt extracted/hello.txt
Compress a single file
# Syntax: arc compress [input file] [output file]

$ arc compress test.txt compressed_test.txt.gz
$ arc compress test.txt gz

For convenience, the output file (second argument) may simply be a compression format (without leading dot), in which case the output filename will be the same as the input filename but with the format extension appended, and the input file will be deleted if successful.

Decompress a single file
# Syntax: arc decompress [input file] [output file]

$ arc decompress test.txt.gz original_test.txt
$ arc decompress test.txt.gz

For convenience, the output file (second argument) may be omitted. In that case, the output filename will have the same name as the input filename, but with the compression extension stripped from the end; and the input file will be deleted if successful.

Flags

Flags are specified before the subcommand. Use arc help or arc -h to get usage help and a description of flags with their default values.

Library Use

The archiver package allows you to easily create and open archives, walk their contents, extract specific files, compress and decompress files, and even stream archives in and out using pure io.Reader and io.Writer interfaces, without ever needing to touch the disk.

import "github.com/mholt/archiver"

See the package's GoDoc for full API documentation.

For example, creating an archive:

z := archiver.Zip{
	CompressionLevel:       flate.DefaultCompression,
	MkdirAll:               true,
	SelectiveCompression:   true,
	ContinueOnError:        false,
	OverwriteExisting:      false,
	ImplicitTopLevelFolder: false,
}

err := z.Archive([]string{"testdata", "other/file.txt"}, "/Users/matt/Desktop/test.zip")

Inspecting an archive:

err = z.Walk("/Users/matt/Desktop/test.zip", func(f archiver.File) error {
	zfh, ok := f.Header.(zip.FileHeader)
	if ok {
		fmt.Println("Filename:", zfh.Name)
	}
	return nil
})

Streaming files into an archive that is being written to the HTTP response:

err = z.Create(responseWriter)
if err != nil {
	return err
}
defer z.Close()

for _, f := range files {
	// ... open file and get the name for it within the archive ...
	err = z.Write(File{
		FileInfo: archiver.FileInfo{
			FileInfo:   f.FileInfo(),
			CustomName: nameInArchive,
		},
		ReadCloser: f,
	})
	if err != nil {
		return err
	}
}

There's a lot more that can be done, too. See the GoDoc for full API documentation.

Security note: This package does NOT attempt to mitigate zip-slip attacks. It is extremely difficult to do properly and seemingly impossible to mitigate effectively across platforms. Attempted fixes have broken processing of legitimate files in production, rendering the program unusable. Our recommendation instead is to inspect the contents of an untrusted archive before extracting it (this package provides Walkers) and decide if you want to proceed with extraction.

Project Values

This project has a few principle-based goals that guide its development:

  • Do our thing really well. Our thing is creating, opening, inspecting, compressing, and streaming archive files. It is not meant to be a replacement for specific archive format tools like tar, zip, etc. that have lots of features and customizability. (Some customizability is OK, but not to the extent that it becomes overly complicated or error-prone.)

  • Have good tests. Changes should be covered by tests.

  • Limit dependencies. Keep the package lightweight.

  • Pure Go. This means no cgo or other external/system dependencies. This package should be able to stand on its own and cross-compile easily to any platform -- and that includes its library dependencies.

  • Idiomatic Go. Keep interfaces small, variable names semantic, vet shows no errors, the linter is generally quiet, etc.

  • Be elegant. This package should be elegant to use and its code should be elegant when reading and testing. If it doesn't feel good, fix it up.

  • Well-documented. Use comments prudently; explain why non-obvious code is necessary (and use tests to enforce it). Keep the docs updated, and have examples where helpful.

  • Keep it efficient. This often means keep it simple. Fast code is valuable.

  • Consensus. Contributions should ideally be approved by multiple reviewers before being merged. Generally, avoid merging multi-chunk changes that do not go through at least one or two iterations/reviews. Except for trivial changes, PRs are seldom ready to merge right away.

  • Have fun contributing. Coding is awesome!

We welcome contributions and appreciate your efforts! However, please open issues to discuss any changes before spending the time preparing a pull request. This will save time, reduce frustration, and help coordinate the work. Thank you!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultRar = &Rar{
	MkdirAll: true,
}

DefaultRar is a convenient archiver ready to use.

View Source
var DefaultTar = &Tar{
	MkdirAll: true,
}

DefaultTar is a convenient archiver ready to use.

View Source
var DefaultTarBz2 = &TarBz2{
	CompressionLevel: bzip2.DefaultCompression,
	Tar:              DefaultTar,
}

DefaultTarBz2 is a convenient archiver ready to use.

View Source
var DefaultTarGz = &TarGz{
	CompressionLevel: gzip.DefaultCompression,
	Tar:              DefaultTar,
}

DefaultTarGz is a convenient archiver ready to use.

View Source
var DefaultTarLz4 = &TarLz4{
	CompressionLevel: 9,
	Tar:              DefaultTar,
}

DefaultTarLz4 is a convenient archiver ready to use.

View Source
var DefaultTarSz = &TarSz{
	Tar: DefaultTar,
}

DefaultTarSz is a convenient archiver ready to use.

View Source
var DefaultTarXz = &TarXz{
	Tar: DefaultTar,
}

DefaultTarXz is a convenient archiver ready to use.

View Source
var DefaultZip = &Zip{
	CompressionLevel:     flate.DefaultCompression,
	MkdirAll:             true,
	SelectiveCompression: true,
}

DefaultZip is a convenient archiver ready to use.

View Source
var ErrStopWalk = fmt.Errorf("walk stopped")

ErrStopWalk signals Walk to break without error.

Functions

This section is empty.

Types

type Archiver

type Archiver interface {
	Archive(sources []string, destination string) error
}

Archiver is a type that can create an archive file from a list of source file names.

type Bz2

type Bz2 struct {
	CompressionLevel int
}

Bz2 facilitates bzip2 compression.

func (*Bz2) CheckExt

func (bz *Bz2) CheckExt(filename string) error

CheckExt ensures the file extension matches the format.

func (*Bz2) Compress

func (bz *Bz2) Compress(in io.Reader, out io.Writer) error

Compress reads in, compresses it, and writes it to out.

func (*Bz2) Decompress

func (bz *Bz2) Decompress(in io.Reader, out io.Writer) error

Decompress reads in, decompresses it, and writes it to out.

func (*Bz2) String

func (bz *Bz2) String() string

type Compressor

type Compressor interface {
	Compress(in io.Reader, out io.Writer) error
	CheckExt(filename string) error
}

Compressor compresses to out what it reads from in. It also ensures a compatible or matching file extension.

type Decompressor

type Decompressor interface {
	Decompress(in io.Reader, out io.Writer) error
}

Decompressor decompresses to out what it reads from in.

type Extractor

type Extractor interface {
	Extract(source, target, destination string) error
}

Extractor can extract a specific file from a source archive to a specific destination folder on disk.

type File

type File struct {
	os.FileInfo

	// The original header info; depends on
	// type of archive -- could be nil, too.
	Header interface{}

	// Allow the file contents to be read (and closed)
	io.ReadCloser
}

File provides methods for accessing information about or contents of a file within an archive.

type FileCompressor

type FileCompressor struct {
	Compressor
	Decompressor

	OverwriteExisting bool
}

FileCompressor can compress and decompress single files.

func (FileCompressor) CompressFile

func (fc FileCompressor) CompressFile(source, destination string) error

CompressFile reads the source file and compresses it to destination. The destination must have a matching extension.

func (FileCompressor) DecompressFile

func (fc FileCompressor) DecompressFile(source, destination string) error

DecompressFile reads the source file and decompresses it to destination.

type FileInfo

type FileInfo struct {
	os.FileInfo
	CustomName string
}

FileInfo is an os.FileInfo but optionally with a custom name, useful if dealing with files that are not actual files on disk, or which have a different name in an archive than on disk.

func (FileInfo) Name

func (fi FileInfo) Name() string

Name returns fi.CustomName if not empty; otherwise it returns fi.FileInfo.Name().

type Gz

type Gz struct {
	CompressionLevel int
}

Gz facilitates gzip compression.

func (*Gz) CheckExt

func (gz *Gz) CheckExt(filename string) error

CheckExt ensures the file extension matches the format.

func (*Gz) Compress

func (gz *Gz) Compress(in io.Reader, out io.Writer) error

Compress reads in, compresses it, and writes it to out.

func (*Gz) Decompress

func (gz *Gz) Decompress(in io.Reader, out io.Writer) error

Decompress reads in, decompresses it, and writes it to out.

func (*Gz) String

func (gz *Gz) String() string

type Lz4

type Lz4 struct {
	CompressionLevel int
}

Lz4 facilitates LZ4 compression.

func (*Lz4) CheckExt

func (lz *Lz4) CheckExt(filename string) error

CheckExt ensures the file extension matches the format.

func (*Lz4) Compress

func (lz *Lz4) Compress(in io.Reader, out io.Writer) error

Compress reads in, compresses it, and writes it to out.

func (*Lz4) Decompress

func (lz *Lz4) Decompress(in io.Reader, out io.Writer) error

Decompress reads in, decompresses it, and writes it to out.

func (*Lz4) String

func (lz *Lz4) String() string

type Matcher

type Matcher interface {
	Match(*os.File) (bool, error)
}

Matcher is a type that can return whether the given file appears to match the implementation's format. Implementations should return the file's read position to where it was when the method was called.

type Rar

type Rar struct {
	// Whether to overwrite existing files; if false,
	// an error is returned if the file exists.
	OverwriteExisting bool

	// Whether to make all the directories necessary
	// to create a rar archive in the desired path.
	MkdirAll bool

	// A single top-level folder can be implicitly
	// created by the Unarchive method if the files
	// to be extracted from the archive do not all
	// have a common root. This roughly mimics the
	// behavior of archival tools integrated into OS
	// file browsers which create a subfolder to
	// avoid unexpectedly littering the destination
	// folder with potentially many files, causing a
	// problematic cleanup/organization situation.
	// This feature is available for both creation
	// and extraction of archives, but may be slightly
	// inefficient with lots and lots of files,
	// especially on extraction.
	ImplicitTopLevelFolder bool

	// If true, errors encountered during reading
	// or writing a single file will be logged and
	// the operation will continue on remaining files.
	ContinueOnError bool

	// The password to open archives (optional).
	Password string
	// contains filtered or unexported fields
}

Rar provides facilities for reading RAR archives. See https://www.rarlab.com/technote.htm.

func (*Rar) Close

func (r *Rar) Close() error

Close closes the rar archive(s) opened by Create and Open.

func (*Rar) Extract

func (r *Rar) Extract(source, target, destination string) error

Extract extracts a single file from the rar archive. If the target is a directory, the entire folder will be extracted into destination.

func (*Rar) Match

func (*Rar) Match(file *os.File) (bool, error)

Match returns true if the format of file matches this type's format. It should not affect reader position.

func (*Rar) Open

func (r *Rar) Open(in io.Reader, size int64) error

Open opens t for reading an archive from in. The size parameter is not used.

func (*Rar) OpenFile

func (r *Rar) OpenFile(filename string) error

OpenFile opens filename for reading. This method supports multi-volume archives, whereas Open does not (but Open supports any stream, not just files).

func (*Rar) Read

func (r *Rar) Read() (File, error)

Read reads the next file from t, which must have already been opened for reading. If there are no more files, the error is io.EOF. The File must be closed when finished reading from it.

func (*Rar) String

func (r *Rar) String() string

func (*Rar) Unarchive

func (r *Rar) Unarchive(source, destination string) error

Unarchive unpacks the .rar file at source to destination. Destination will be treated as a folder name. It supports multi-volume archives.

func (*Rar) Walk

func (r *Rar) Walk(archive string, walkFn WalkFunc) error

Walk calls walkFn for each visited item in archive.

type ReadFakeCloser

type ReadFakeCloser struct {
	io.Reader
}

ReadFakeCloser is an io.Reader that has a no-op close method to satisfy the io.ReadCloser interface.

func (ReadFakeCloser) Close

func (rfc ReadFakeCloser) Close() error

Close implements io.Closer.

type Reader

type Reader interface {
	Open(in io.Reader, size int64) error
	Read() (File, error)
	Close() error
}

Reader can read discrete byte streams of files from an input stream.

type Snappy

type Snappy struct{}

Snappy facilitates Snappy compression.

func (*Snappy) CheckExt

func (s *Snappy) CheckExt(filename string) error

CheckExt ensures the file extension matches the format.

func (*Snappy) Compress

func (s *Snappy) Compress(in io.Reader, out io.Writer) error

Compress reads in, compresses it, and writes it to out.

func (*Snappy) Decompress

func (s *Snappy) Decompress(in io.Reader, out io.Writer) error

Decompress reads in, decompresses it, and writes it to out.

func (*Snappy) String

func (s *Snappy) String() string

type Tar

type Tar struct {
	// Whether to overwrite existing files; if false,
	// an error is returned if the file exists.
	OverwriteExisting bool

	// Whether to make all the directories necessary
	// to create a tar archive in the desired path.
	MkdirAll bool

	// A single top-level folder can be implicitly
	// created by the Archive or Unarchive methods
	// if the files to be added to the archive
	// or the files to be extracted from the archive
	// do not all have a common root. This roughly
	// mimics the behavior of archival tools integrated
	// into OS file browsers which create a subfolder
	// to avoid unexpectedly littering the destination
	// folder with potentially many files, causing a
	// problematic cleanup/organization situation.
	// This feature is available for both creation
	// and extraction of archives, but may be slightly
	// inefficient with lots and lots of files,
	// especially on extraction.
	ImplicitTopLevelFolder bool

	// If true, errors encountered during reading
	// or writing a single file will be logged and
	// the operation will continue on remaining files.
	ContinueOnError bool
	// contains filtered or unexported fields
}

Tar provides facilities for operating TAR archives. See http://www.gnu.org/software/tar/manual/html_node/Standard.html.

func (*Tar) Archive

func (t *Tar) Archive(sources []string, destination string) error

Archive creates a tarball file at destination containing the files listed in sources. The destination must end with ".tar". File paths can be those of regular files or directories; directories will be recursively added.

func (*Tar) Close

func (t *Tar) Close() error

Close closes the tar archive(s) opened by Create and Open.

func (*Tar) Create

func (t *Tar) Create(out io.Writer) error

Create opens t for writing a tar archive to out.

func (*Tar) Extract

func (t *Tar) Extract(source, target, destination string) error

Extract extracts a single file from the tar archive. If the target is a directory, the entire folder will be extracted into destination.

func (*Tar) Match

func (*Tar) Match(file *os.File) (bool, error)

Match returns true if the format of file matches this type's format. It should not affect reader position.

func (*Tar) Open

func (t *Tar) Open(in io.Reader, size int64) error

Open opens t for reading an archive from in. The size parameter is not used.

func (*Tar) Read

func (t *Tar) Read() (File, error)

Read reads the next file from t, which must have already been opened for reading. If there are no more files, the error is io.EOF. The File must be closed when finished reading from it.

func (*Tar) String

func (t *Tar) String() string

func (*Tar) Unarchive

func (t *Tar) Unarchive(source, destination string) error

Unarchive unpacks the .tar file at source to destination. Destination will be treated as a folder name.

func (*Tar) Walk

func (t *Tar) Walk(archive string, walkFn WalkFunc) error

Walk calls walkFn for each visited item in archive.

func (*Tar) Write

func (t *Tar) Write(f File) error

Write writes f to t, which must have been opened for writing first.

type TarBz2

type TarBz2 struct {
	*Tar

	CompressionLevel int
}

TarBz2 facilitates bzip2 compression (https://github.com/dsnet/compress/blob/master/doc/bzip2-format.pdf) of tarball archives.

func (*TarBz2) Archive

func (tbz2 *TarBz2) Archive(sources []string, destination string) error

Archive creates a compressed tar file at destination containing the files listed in sources. The destination must end with ".tar.bz2" or ".tbz2". File paths can be those of regular files or directories; directories will be recursively added.

func (*TarBz2) Create

func (tbz2 *TarBz2) Create(out io.Writer) error

Create opens tbz2 for writing a compressed tar archive to out.

func (*TarBz2) Extract

func (tbz2 *TarBz2) Extract(source, target, destination string) error

Extract extracts a single file from the tar archive. If the target is a directory, the entire folder will be extracted into destination.

func (*TarBz2) Open

func (tbz2 *TarBz2) Open(in io.Reader, size int64) error

Open opens t for reading a compressed archive from in. The size parameter is not used.

func (*TarBz2) String

func (tbz2 *TarBz2) String() string

func (*TarBz2) Unarchive

func (tbz2 *TarBz2) Unarchive(source, destination string) error

Unarchive unpacks the compressed tarball at source to destination. Destination will be treated as a folder name.

func (*TarBz2) Walk

func (tbz2 *TarBz2) Walk(archive string, walkFn WalkFunc) error

Walk calls walkFn for each visited item in archive.

type TarGz

type TarGz struct {
	*Tar

	// The compression level to use, as described
	// in the compress/gzip package.
	CompressionLevel int
}

TarGz facilitates gzip compression (RFC 1952) of tarball archives.

func (*TarGz) Archive

func (tgz *TarGz) Archive(sources []string, destination string) error

Archive creates a compressed tar file at destination containing the files listed in sources. The destination must end with ".tar.gz" or ".tgz". File paths can be those of regular files or directories; directories will be recursively added.

func (*TarGz) Create

func (tgz *TarGz) Create(out io.Writer) error

Create opens txz for writing a compressed tar archive to out.

func (*TarGz) Extract

func (tgz *TarGz) Extract(source, target, destination string) error

Extract extracts a single file from the tar archive. If the target is a directory, the entire folder will be extracted into destination.

func (*TarGz) Open

func (tgz *TarGz) Open(in io.Reader, size int64) error

Open opens t for reading a compressed archive from in. The size parameter is not used.

func (*TarGz) String

func (tgz *TarGz) String() string

func (*TarGz) Unarchive

func (tgz *TarGz) Unarchive(source, destination string) error

Unarchive unpacks the compressed tarball at source to destination. Destination will be treated as a folder name.

func (*TarGz) Walk

func (tgz *TarGz) Walk(archive string, walkFn WalkFunc) error

Walk calls walkFn for each visited item in archive.

type TarLz4

type TarLz4 struct {
	*Tar

	// The compression level to use when writing.
	// Minimum 0 (fast compression), maximum 12
	// (most space savings).
	CompressionLevel int
}

TarLz4 facilitates lz4 compression (https://github.com/lz4/lz4/tree/master/doc) of tarball archives.

func (*TarLz4) Archive

func (tlz4 *TarLz4) Archive(sources []string, destination string) error

Archive creates a compressed tar file at destination containing the files listed in sources. The destination must end with ".tar.lz4" or ".tlz4". File paths can be those of regular files or directories; directories will be recursively added.

func (*TarLz4) Create

func (tlz4 *TarLz4) Create(out io.Writer) error

Create opens tlz4 for writing a compressed tar archive to out.

func (*TarLz4) Extract

func (tlz4 *TarLz4) Extract(source, target, destination string) error

Extract extracts a single file from the tar archive. If the target is a directory, the entire folder will be extracted into destination.

func (*TarLz4) Open

func (tlz4 *TarLz4) Open(in io.Reader, size int64) error

Open opens t for reading a compressed archive from in. The size parameter is not used.

func (*TarLz4) String

func (tlz4 *TarLz4) String() string

func (*TarLz4) Unarchive

func (tlz4 *TarLz4) Unarchive(source, destination string) error

Unarchive unpacks the compressed tarball at source to destination. Destination will be treated as a folder name.

func (*TarLz4) Walk

func (tlz4 *TarLz4) Walk(archive string, walkFn WalkFunc) error

Walk calls walkFn for each visited item in archive.

type TarSz

type TarSz struct {
	*Tar
}

TarSz facilitates Snappy compression (https://github.com/google/snappy) of tarball archives.

func (*TarSz) Archive

func (tsz *TarSz) Archive(sources []string, destination string) error

Archive creates a compressed tar file at destination containing the files listed in sources. The destination must end with ".tar.sz" or ".tsz". File paths can be those of regular files or directories; directories will be recursively added.

func (*TarSz) Create

func (tsz *TarSz) Create(out io.Writer) error

Create opens tsz for writing a compressed tar archive to out.

func (*TarSz) Extract

func (tsz *TarSz) Extract(source, target, destination string) error

Extract extracts a single file from the tar archive. If the target is a directory, the entire folder will be extracted into destination.

func (*TarSz) Open

func (tsz *TarSz) Open(in io.Reader, size int64) error

Open opens t for reading a compressed archive from in. The size parameter is not used.

func (*TarSz) String

func (tsz *TarSz) String() string

func (*TarSz) Unarchive

func (tsz *TarSz) Unarchive(source, destination string) error

Unarchive unpacks the compressed tarball at source to destination. Destination will be treated as a folder name.

func (*TarSz) Walk

func (tsz *TarSz) Walk(archive string, walkFn WalkFunc) error

Walk calls walkFn for each visited item in archive.

type TarXz

type TarXz struct {
	*Tar
}

TarXz facilitates xz compression (https://tukaani.org/xz/format.html) of tarball archives.

func (*TarXz) Archive

func (txz *TarXz) Archive(sources []string, destination string) error

Archive creates a compressed tar file at destination containing the files listed in sources. The destination must end with ".tar.gz" or ".txz". File paths can be those of regular files or directories; directories will be recursively added.

func (*TarXz) Create

func (txz *TarXz) Create(out io.Writer) error

Create opens txz for writing a compressed tar archive to out.

func (*TarXz) Extract

func (txz *TarXz) Extract(source, target, destination string) error

Extract extracts a single file from the tar archive. If the target is a directory, the entire folder will be extracted into destination.

func (*TarXz) Open

func (txz *TarXz) Open(in io.Reader, size int64) error

Open opens t for reading a compressed archive from in. The size parameter is not used.

func (*TarXz) String

func (txz *TarXz) String() string

func (*TarXz) Unarchive

func (txz *TarXz) Unarchive(source, destination string) error

Unarchive unpacks the compressed tarball at source to destination. Destination will be treated as a folder name.

func (*TarXz) Walk

func (txz *TarXz) Walk(archive string, walkFn WalkFunc) error

Walk calls walkFn for each visited item in archive.

type Unarchiver

type Unarchiver interface {
	Unarchive(source, destination string) error
}

Unarchiver is a type that can extract archive files into a folder.

type WalkFunc

type WalkFunc func(f File) error

WalkFunc is called at each item visited by Walk. If an error is returned, the walk may continue if the Walker is configured to continue on error. The sole exception is the error value ErrStopWalk, which stops the walk without an actual error.

type Walker

type Walker interface {
	Walk(archive string, walkFn WalkFunc) error
}

Walker can walk an archive file and return information about each item in the archive.

type Writer

type Writer interface {
	Create(out io.Writer) error
	Write(f File) error
	Close() error
}

Writer can write discrete byte streams of files to an output stream.

type Xz

type Xz struct{}

Xz facilitates XZ compression.

func (*Xz) CheckExt

func (x *Xz) CheckExt(filename string) error

CheckExt ensures the file extension matches the format.

func (*Xz) Compress

func (x *Xz) Compress(in io.Reader, out io.Writer) error

Compress reads in, compresses it, and writes it to out.

func (*Xz) Decompress

func (x *Xz) Decompress(in io.Reader, out io.Writer) error

Decompress reads in, decompresses it, and writes it to out.

func (*Xz) String

func (x *Xz) String() string

type Zip

type Zip struct {
	// The compression level to use, as described
	// in the compress/flate package.
	CompressionLevel int

	// Whether to overwrite existing files; if false,
	// an error is returned if the file exists.
	OverwriteExisting bool

	// Whether to make all the directories necessary
	// to create a zip archive in the desired path.
	MkdirAll bool

	// If enabled, selective compression will only
	// compress files which are not already in a
	// compressed format; this is decided based
	// simply on file extension.
	SelectiveCompression bool

	// A single top-level folder can be implicitly
	// created by the Archive or Unarchive methods
	// if the files to be added to the archive
	// or the files to be extracted from the archive
	// do not all have a common root. This roughly
	// mimics the behavior of archival tools integrated
	// into OS file browsers which create a subfolder
	// to avoid unexpectedly littering the destination
	// folder with potentially many files, causing a
	// problematic cleanup/organization situation.
	// This feature is available for both creation
	// and extraction of archives, but may be slightly
	// inefficient with lots and lots of files,
	// especially on extraction.
	ImplicitTopLevelFolder bool

	// If true, errors encountered during reading
	// or writing a single file will be logged and
	// the operation will continue on remaining files.
	ContinueOnError bool
	// contains filtered or unexported fields
}

Zip provides facilities for operating ZIP archives. See https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT.

func (*Zip) Archive

func (z *Zip) Archive(sources []string, destination string) error

Archive creates a .zip file at destination containing the files listed in sources. The destination must end with ".zip". File paths can be those of regular files or directories. Regular files are stored at the 'root' of the archive, and directories are recursively added.

func (*Zip) Close

func (z *Zip) Close() error

Close closes the zip archive(s) opened by Create and Open.

func (*Zip) Create

func (z *Zip) Create(out io.Writer) error

Create opens z for writing a ZIP archive to out.

func (*Zip) Extract

func (z *Zip) Extract(source, target, destination string) error

Extract extracts a single file from the zip archive. If the target is a directory, the entire folder will be extracted into destination.

func (*Zip) Match

func (*Zip) Match(file *os.File) (bool, error)

Match returns true if the format of file matches this type's format. It should not affect reader position.

func (*Zip) Open

func (z *Zip) Open(in io.Reader, size int64) error

Open opens z for reading an archive from in, which is expected to have the given size and which must be an io.ReaderAt.

func (*Zip) Read

func (z *Zip) Read() (File, error)

Read reads the next file from z, which must have already been opened for reading. If there are no more files, the error is io.EOF. The File must be closed when finished reading from it.

func (*Zip) String

func (z *Zip) String() string

func (*Zip) Unarchive

func (z *Zip) Unarchive(source, destination string) error

Unarchive unpacks the .zip file at source to destination. Destination will be treated as a folder name.

func (*Zip) Walk

func (z *Zip) Walk(archive string, walkFn WalkFunc) error

Walk calls walkFn for each visited item in archive.

func (*Zip) Write

func (z *Zip) Write(f File) error

Write writes f to z, which must have been opened for writing first.

Directories

Path Synopsis
cmd
arc

Jump to

Keyboard shortcuts

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