archiver

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: Unlicense Imports: 16 Imported by: 0

README

Archiver

A golang library for extracting and creating archives.

Documentation Link

The whole goal with this library is to create something that can both read and write to the archives themselves.

Roadmap

  • Automatically detect the archive format
  • Add mutexes to work with concurrency
  • Support RAR format
  • Support zstd format
  • Add writing capability
  • Support multithreaded gzip

Supported Formats

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

Documentation

Index

Constants

View Source
const (
	TarGzip Type = "tar.gz"
	TarBzip      = "tar.bz2"
	TarXz        = "tar.xz"
	Tar          = "tar"
	Zip          = "zip"
)

Variables

View Source
var (
	ErrArchiveTypeNotSupported = errors.New("archive type not supported")
	ErrArchiveFileNotFound     = errors.New("archive file not found")
)

Functions

func Extract

func Extract(path string, options ExtractOptions) error

Extract opens an archive file and extracts the contents. It takes a path, and ExtractOptions for its parameters. The function returns an error, if any.

func ExtractFile

func ExtractFile(path string, file string, options ExtractFileOptions) error

ExtractFile opens an archive file and extracts a specific file within the archive. It takes a path, a file path, and ExtractFileOptions for its parameters. The function returns an error, if any.

Types

type Archive

type Archive struct {
	// Type of Archive
	Type Type
	// Path of Archive file
	Path string
	// contains filtered or unexported fields
}

func Open

func Open(path string) (*Archive, error)

Open opens an archive file and returns an Archive struct. It takes a path to the archive file as its parameter. The function returns an Archive struct and an error, if any.

func OpenFS

func OpenFS(filesystem Filesystem, path string) (*Archive, error)

OpenFS opens an archive file from a given fs.FS and returns an Archive struct. It takes fs.FS, and a path to the archive file as its parameters. The function returns an Archive struct and an error, if any.

func (*Archive) Close

func (a *Archive) Close() error

Close closes the Archive. The function returns an error, if any.

func (*Archive) Extract

func (a *Archive) Extract(opts ExtractOptions) error

Extract extracts the specified Archive to a specified directory. It takes ExtractOptions as its parameter The function returns an error, if any.

func (*Archive) ExtractBillyFS

func (a *Archive) ExtractBillyFS(filesystem billy.Filesystem, opts ExtractOptions) error

ExtractBillyFS extracts the specified Archive into billy.Filesystem. It takes billy.Filesystem and ExtractOptions as its parameters. The function returns an error, if any.

func (*Archive) ExtractFile

func (a *Archive) ExtractFile(path string, opts ExtractFileOptions) error

ExtractFile extracts a specified file from an archive to a specified directory. It takes a file path, and ExtractFileOptions as its parameters. The function returns an error, if any.

func (*Archive) FileCount

func (a *Archive) FileCount() int

FileCount gets the total file count in the Archive. The function returns an integer.

func (*Archive) GetFile

func (a *Archive) GetFile(path string) (*File, error)

GetFile gets a File from the Archive It takes the path of the file in the Archive as its parameter. The function returns ArchiveFile and an error, if any.

type ExtractFileOptions

type ExtractFileOptions struct {
	// Whether to overwrite files if they already exist.
	Overwrite bool
	// Perms for output file, default is the permission in the Archive.
	Perms os.FileMode
	// Folder to extract Archive to.
	Folder string
	// Whether to preserve the file structure in the Archive or not.
	NotPreserveFileStructure bool
}

ExtractFileOptions represents options for extracting an ArchiveFile

type ExtractOptions

type ExtractOptions struct {
	// Whether to overwrite files if they already exist.
	Overwrite bool
	// Folder to extract Archive to.
	Folder string
	// Whether to preserve the file structure in the Archive or not.
	NotPreserveFileStructure bool
	// Optional regex filter for specific files.
	Filter *regexp.Regexp
}

ExtractOptions represents options for extracting an Archive

type File

type File struct {
	// Name of file (e.g "test.txt")
	FileName string
	// Path of file in archive (e.g "folder/test.txt")
	Path string
	// contains filtered or unexported fields
}

File represents a file in an archive.

func (*File) Extract

func (f *File) Extract(opts ExtractFileOptions) error

Extract extracts the specified File in the archive to a specified directory. It takes ExtractFileOptions as its parameter. The function returns an error, if any.

func (*File) ExtractBillyFS

func (f *File) ExtractBillyFS(filesystem billy.Filesystem, opts ExtractFileOptions) error

ExtractBillyFS extracts the specified File in the archive into billy.Filesystem. It takes billy.Filesystem and ExtractFileOptions as its parameters. The function returns an error, if any.

func (*File) Read

func (f *File) Read() ([]byte, error)

Read reads the specified File in the archive and returns the content. The function returns the content of the File, and an error, if any.

type Filesystem

type Filesystem struct {
	// contains filtered or unexported fields
}

Filesystem represents a standard interface for filesystems. Currently, supports fs.FS and billy.Filesystem.

func WrapBillyFS

func WrapBillyFS(filesystem billy.Filesystem) Filesystem

WrapBillyFS wraps billy.Filesystem to the Filesystem interface. Returns Filesystem.

func WrapFS

func WrapFS(filesystem fs.FS) Filesystem

WrapFS wraps fs.FS to the Filesystem interface. Returns Filesystem.

func WrapPath

func WrapPath(path string) Filesystem

WrapPath wraps a path to the Filesystem interface. Returns Filesystem.

type Type

type Type string

Jump to

Keyboard shortcuts

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