Documentation ¶
Index ¶
- Constants
- Variables
- func FileSystem(ctx context.Context, root string) (fs.FS, error)
- func RegisterFormat(format Format)
- func TopDirOpen(fsys fs.FS, name string) (fs.File, error)
- func TopDirReadDir(fsys fs.FS, name string) ([]fs.DirEntry, error)
- func TopDirStat(fsys fs.FS, name string) (fs.FileInfo, error)
- type Archival
- type ArchiveAsyncJob
- type ArchiveFS
- type Archiver
- type ArchiverAsync
- type Brotli
- type Bz2
- type CompressedArchive
- func (caf CompressedArchive) Archive(ctx context.Context, output io.Writer, files []File) error
- func (caf CompressedArchive) ArchiveAsync(ctx context.Context, output io.Writer, jobs <-chan ArchiveAsyncJob) error
- func (caf CompressedArchive) Extract(ctx context.Context, sourceArchive io.Reader, pathsInArchive []string, ...) error
- func (caf CompressedArchive) Match(filename string, stream io.Reader) (MatchResult, error)
- func (caf CompressedArchive) Name() string
- type Compression
- type Compressor
- type Decompressor
- type DirFS
- type Extractor
- type File
- type FileFS
- type FileHandler
- type Format
- type FromDiskOptions
- type Gz
- type Inserter
- type Lz4
- type MatchResult
- type Rar
- type SevenZip
- type Sz
- type Tar
- func (t Tar) Archive(ctx context.Context, output io.Writer, files []File) error
- func (t Tar) ArchiveAsync(ctx context.Context, output io.Writer, jobs <-chan ArchiveAsyncJob) error
- func (t Tar) Extract(ctx context.Context, sourceArchive io.Reader, pathsInArchive []string, ...) error
- func (t Tar) Insert(ctx context.Context, into io.ReadWriteSeeker, files []File) error
- func (t Tar) Match(filename string, stream io.Reader) (MatchResult, error)
- func (Tar) Name() string
- type Xz
- type Zip
- func (z Zip) Archive(ctx context.Context, output io.Writer, files []File) error
- func (z Zip) ArchiveAsync(ctx context.Context, output io.Writer, jobs <-chan ArchiveAsyncJob) error
- func (z Zip) Extract(ctx context.Context, sourceArchive io.Reader, pathsInArchive []string, ...) error
- func (z Zip) Match(filename string, stream io.Reader) (MatchResult, error)
- func (z Zip) Name() string
- type Zlib
- type Zstd
Constants ¶
const ( ZipMethodBzip2 = 12 // TODO: LZMA: Disabled - because 7z isn't able to unpack ZIP+LZMA ZIP+LZMA2 archives made this way - and vice versa. // ZipMethodLzma = 14 ZipMethodZstd = 93 ZipMethodXz = 95 )
Additional compression methods not offered by archive/zip. See https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT section 4.4.5.
Variables ¶
var ErrNoMatch = fmt.Errorf("no formats matched")
ErrNoMatch is returned if there are no matching formats.
var ZlibHeader = []byte{0x78}
Functions ¶
func FileSystem ¶
FileSystem opens the file at root as a read-only file system. The root may be a path to a directory, archive file, compressed archive file, compressed file, or any other file on disk.
If root is a directory, its contents are accessed directly from the disk's file system. If root is an archive file, its contents can be accessed like a normal directory; compressed archive files are transparently decompressed as contents are accessed. And if root is any other file, it is the only file in the file system; if the file is compressed, it is transparently decompressed when read from.
This method essentially offers uniform read access to various kinds of files: directories, archives, compressed archives, and individual files are all treated the same way.
Except for zip files, the returned FS values are guaranteed to be fs.ReadDirFS and fs.StatFS types, and may also be fs.SubFS.
func RegisterFormat ¶
func RegisterFormat(format Format)
RegisterFormat registers a format. It should be called during init. Duplicate formats by name are not allowed and will panic.
func TopDirOpen ¶
TopDirOpen is a special Open() function that may be useful if a file system root was created by extracting an archive.
It first tries the file name as given, but if that returns an error, it tries the name without the first element of the path. In other words, if "a/b/c" returns an error, then "b/c" will be tried instead.
Consider an archive that contains a file "a/b/c". When the archive is extracted, the contents may be created without a new parent/root folder to contain them, and the path of the same file outside the archive may be lacking an exclusive root or parent container. Thus it is likely for a file system created for the same files extracted to disk to be rooted at one of the top-level files/folders from the archive instead of a parent folder. For example, the file known as "a/b/c" when rooted at the archive becomes "b/c" after extraction when rooted at "a" on disk (because no new, exclusive top-level folder was created). This difference in paths can make it difficult to use archives and directories uniformly. Hence these TopDir* functions which attempt to smooth over the difference.
Some extraction utilities do create a container folder for archive contents when extracting, in which case the user may give that path as the root. In that case, these TopDir* functions are not necessary (but aren't harmful either). They are primarily useful if you are not sure whether the root is an archive file or is an extracted archive file, as they will work with the same filename/path inputs regardless of the presence of a top-level directory.
func TopDirReadDir ¶
TopDirReadDir is like TopDirOpen but for ReadDir.
Types ¶
type ArchiveAsyncJob ¶
ArchiveAsyncJob contains a File to be archived and a channel that the result of the archiving should be returned on.
type ArchiveFS ¶
type ArchiveFS struct { // set one of these Path string // path to the archive file on disk, or... Stream *io.SectionReader // ...stream from which to read archive Format Archival // the archive format Prefix string // optional subdirectory in which to root the fs Context context.Context // optional }
ArchiveFS allows accessing an archive (or a compressed archive) using a consistent file system interface. Essentially, it allows traversal and reading of archive contents the same way as any normal directory on disk. The contents of compressed archives are transparently decompressed.
A valid ArchiveFS value must set either Path or Stream. If Path is set, a literal file will be opened from the disk. If Stream is set, new SectionReaders will be implicitly created to access the stream, enabling safe, concurrent access.
NOTE: Due to Go's file system APIs (see package io/fs), the performance of ArchiveFS when used with fs.WalkDir() is poor for archives with lots of files (see issue #326). The fs.WalkDir() API requires listing each directory's contents in turn, and the only way to ensure we return the complete list of folder contents is to traverse the whole archive and build a slice; so if this is done for the root of an archive with many files, performance tends toward O(n^2) as the entire archive is walked for every folder that is enumerated (WalkDir calls ReadDir recursively). If you do not need each directory's contents walked in order, please prefer calling Extract() from an archive type directly; this will perform a O(n) walk of the contents in archive order, rather than the slower directory tree order.
func (ArchiveFS) Open ¶
Open opens the named file from within the archive. If name is "." then the archive file itself will be opened as a directory file.
type Archiver ¶
type Archiver interface { // Archive writes an archive file to output with the given files. // // Context cancellation must be honored. Archive(ctx context.Context, output io.Writer, files []File) error }
Archiver can create a new archive.
type ArchiverAsync ¶
type ArchiverAsync interface { Archiver // Use ArchiveAsync if you can't pre-assemble a list of all // the files for the archive. Close the jobs channel after // all the files have been sent. // // This won't return until the channel is closed. ArchiveAsync(ctx context.Context, output io.Writer, jobs <-chan ArchiveAsyncJob) error }
ArchiverAsync is an Archiver that can also create archives asynchronously by pumping files into a channel as they are discovered.
type Brotli ¶
type Brotli struct {
Quality int
}
Brotli facilitates brotli compression.
func (Brotli) OpenReader ¶
func (Brotli) OpenWriter ¶
type Bz2 ¶
type Bz2 struct {
CompressionLevel int
}
Bz2 facilitates bzip2 compression.
func (Bz2) OpenReader ¶
func (Bz2) OpenWriter ¶
type CompressedArchive ¶
type CompressedArchive struct { Compression Archival }
CompressedArchive combines a compression format on top of an archive format (e.g. "tar.gz") and provides both functionalities in a single type. It ensures that archive functions are wrapped by compressors and decompressors. However, compressed archives have some limitations; for example, files cannot be inserted/appended because of complexities with modifying existing compression state (perhaps this could be overcome, but I'm not about to try it).
As this type is intended to compose compression and archive formats, both must be specified in order for this value to be valid, or its methods will return errors.
func (CompressedArchive) Archive ¶
Archive adds files to the output archive while compressing the result.
func (CompressedArchive) ArchiveAsync ¶
func (caf CompressedArchive) ArchiveAsync(ctx context.Context, output io.Writer, jobs <-chan ArchiveAsyncJob) error
ArchiveAsync adds files to the output archive while compressing the result asynchronously.
func (CompressedArchive) Extract ¶
func (caf CompressedArchive) Extract(ctx context.Context, sourceArchive io.Reader, pathsInArchive []string, handleFile FileHandler) error
Extract reads files out of an archive while decompressing the results.
func (CompressedArchive) Match ¶
func (caf CompressedArchive) Match(filename string, stream io.Reader) (MatchResult, error)
Match matches if the input matches both the compression and archive format.
func (CompressedArchive) Name ¶
func (caf CompressedArchive) Name() string
Name returns a concatenation of the archive format name and the compression format name.
type Compression ¶
type Compression interface { Format Compressor Decompressor }
Compression is a compression format with both compress and decompress methods.
type Compressor ¶
type Compressor interface { // OpenWriter wraps w with a new writer that compresses what is written. // The writer must be closed when writing is finished. OpenWriter(w io.Writer) (io.WriteCloser, error) }
Compressor can compress data by wrapping a writer.
type Decompressor ¶
type Decompressor interface { // OpenReader wraps r with a new reader that decompresses what is read. // The reader must be closed when reading is finished. OpenReader(r io.Reader) (io.ReadCloser, error) }
Decompressor can decompress data by wrapping a reader.
type DirFS ¶
type DirFS string
DirFS allows accessing a directory on disk with a consistent file system interface. It is almost the same as os.DirFS, except for some reason os.DirFS only implements Open() and Stat(), but we also need ReadDir(). Seems like an obvious miss (as of Go 1.17) and I have questions: https://twitter.com/mholt6/status/1476058551432876032
type Extractor ¶
type Extractor interface { // Extract reads the files at pathsInArchive from sourceArchive. // If pathsInArchive is nil, all files are extracted without discretion. // If pathsInArchive is empty, no files are extracted. // If a path refers to a directory, all files within it are extracted. // Extracted files are passed to the handleFile callback for handling. // // Context cancellation must be honored. Extract(ctx context.Context, sourceArchive io.Reader, pathsInArchive []string, handleFile FileHandler) error }
Extractor can extract files from an archive.
type File ¶
type File struct { fs.FileInfo // The file header as used/provided by the archive format. // Typically, you do not need to set this field when creating // an archive. Header interface{} // The path of the file as it appears in the archive. // This is equivalent to Header.Name (for most Header // types). We require it to be specified here because // it is such a common field and we want to preserve // format-agnosticism (no type assertions) for basic // operations. NameInArchive string // For symbolic and hard links, the target of the link. // Not supported by all archive formats. LinkTarget string // A callback function that opens the file to read its // contents. The file must be closed when reading is // complete. Nil for files that don't have content // (such as directories and links). Open func() (io.ReadCloser, error) }
File is a virtualized, generalized file abstraction for interacting with archives.
func FilesFromDisk ¶
func FilesFromDisk(options *FromDiskOptions, filenames map[string]string) ([]File, error)
FilesFromDisk returns a list of files by walking the directories in the given filenames map. The keys are the names on disk, and the values are their associated names in the archive.
Map keys that specify directories on disk will be walked and added to the archive recursively, rooted at the named directory. They should use the platform's path separator (backslash on Windows; slash on everything else). For convenience, map keys that end in a separator ('/', or '\' on Windows) will enumerate contents only without adding the folder itself to the archive.
Map values should typically use slash ('/') as the separator regardless of the platform, as most archive formats standardize on that rune as the directory separator for filenames within an archive. For convenience, map values that are empty string are interpreted as the base name of the file (sans path) in the root of the archive; and map values that end in a slash will use the base name of the file in that folder of the archive.
File gathering will adhere to the settings specified in options.
This function is used primarily when preparing a list of files to add to an archive.
type FileFS ¶
type FileFS struct { // The path to the file on disk. Path string // If file is compressed, setting this field will // transparently decompress reads. Compression Decompressor }
FileFS allows accessing a file on disk using a consistent file system interface. The value should be the path to a regular file, not a directory. This file will be the only entry in the file system and will be at its root. It can be accessed within the file system by the name of "." or the filename.
If the file is compressed, set the Compression field so that reads from the file will be transparently decompressed.
func (FileFS) Open ¶
Open opens the named file, which must be the file used to create the file system.
type FileHandler ¶
FileHandler is a callback function that is used to handle files as they are read from an archive; it is kind of like fs.WalkDirFunc. Handler functions that open their files must not overlap or run concurrently, as files may be read from the same sequential stream; always close the file before returning.
If the special error value fs.SkipDir is returned, the directory of the file (or the file itself if it is a directory) will not be walked. Note that because archive contents are not necessarily ordered, skipping directories requires memory, and skipping lots of directories may run up your memory bill.
Any other returned error will terminate a walk.
type Format ¶
type Format interface { // Name returns the name of the format. Name() string // Match returns true if the given name/stream is recognized. // One of the arguments is optional: filename might be empty // if working with an unnamed stream, or stream might be // empty if only working with a filename. The filename should // consist only of the base name, not a path component, and is // typically used for matching by file extension. However, // matching by reading the stream is preferred. Match reads // only as many bytes as needed to determine a match. To // preserve the stream through matching, you should either // buffer what is read by Match, or seek to the last position // before Match was called. Match(filename string, stream io.Reader) (MatchResult, error) }
Format represents either an archive or compression format.
func Identify ¶
Identify iterates the registered formats and returns the one that matches the given filename and/or stream. It is capable of identifying compressed files (.gz, .xz...), archive files (.tar, .zip...), and compressed archive files (tar.gz, tar.bz2...). The returned Format value can be type-asserted to ascertain its capabilities.
If no matching formats were found, special error ErrNoMatch is returned.
If stream is nil then it will only match on file name and the returned io.Reader will be nil.
If stream is non-nil then the returned io.Reader will always be non-nil and will read from the same point as the reader which was passed in; it should be used in place of the input stream after calling Identify() because it preserves and re-reads the bytes that were already read during the identification process.
type FromDiskOptions ¶
type FromDiskOptions struct { // If true, symbolic links will be dereferenced, meaning that // the link will not be added as a link, but what the link // points to will be added as a file. FollowSymlinks bool // If true, some file attributes will not be preserved. // Name, size, type, and permissions will still be preserved. ClearAttributes bool }
FromDiskOptions specifies various options for gathering files from disk.
type Gz ¶
type Gz struct { // Gzip compression level. See https://pkg.go.dev/compress/flate#pkg-constants // for some predefined constants. If 0, DefaultCompression is assumed rather // than no compression. CompressionLevel int // Use a fast parallel Gzip implementation. This is only // effective for large streams (about 1 MB or greater). Multithreaded bool }
Gz facilitates gzip compression.
func (Gz) OpenReader ¶
func (Gz) OpenWriter ¶
type Inserter ¶
type Inserter interface { // Insert inserts the files into archive. // // Context cancellation must be honored. Insert(ctx context.Context, archive io.ReadWriteSeeker, files []File) error }
Inserter can insert files into an existing archive.
type Lz4 ¶
type Lz4 struct {
CompressionLevel int
}
Lz4 facilitates LZ4 compression.
func (Lz4) OpenReader ¶
func (Lz4) OpenWriter ¶
type MatchResult ¶
type MatchResult struct {
ByName, ByStream bool
}
MatchResult returns true if the format was matched either by name, stream, or both. Name usually refers to matching by file extension, and stream usually refers to reading the first few bytes of the stream (its header). A stream match is generally stronger, as filenames are not always indicative of their contents if they even exist at all.
func (MatchResult) Matched ¶
func (mr MatchResult) Matched() bool
Matched returns true if a match was made by either name or stream.
type Rar ¶
type Rar struct { // If true, errors encountered during reading or writing // a file within an archive will be logged and the // operation will continue on remaining files. ContinueOnError bool // Password to open archives. Password string }
func (Rar) Archive ¶
Archive is not implemented for RAR, but the method exists so that Rar satisfies the ArchiveFormat interface.
type SevenZip ¶
type SevenZip struct { // If true, errors encountered during reading or writing // a file within an archive will be logged and the // operation will continue on remaining files. ContinueOnError bool // The password, if dealing with an encrypted archive. Password string }
func (SevenZip) Archive ¶
Archive is not implemented for 7z, but the method exists so that SevenZip satisfies the ArchiveFormat interface.
func (SevenZip) Extract ¶
func (z SevenZip) Extract(ctx context.Context, sourceArchive io.Reader, pathsInArchive []string, handleFile FileHandler) error
Extract extracts files from z, implementing the Extractor interface. Uniquely, however, sourceArchive must be an io.ReaderAt and io.Seeker, which are oddly disjoint interfaces from io.Reader which is what the method signature requires. We chose this signature for the interface because we figure you can Read() from anything you can ReadAt() or Seek() with. Due to the nature of the zip archive format, if sourceArchive is not an io.Seeker and io.ReaderAt, an error is returned.
type Sz ¶
type Sz struct{}
Sz facilitates Snappy compression.
func (Sz) OpenReader ¶
func (Sz) OpenWriter ¶
type Tar ¶
type Tar struct { // If true, errors encountered during reading or writing // a file within an archive will be logged and the // operation will continue on remaining files. ContinueOnError bool }
func (Tar) ArchiveAsync ¶
type Xz ¶
type Xz struct{}
Xz facilitates xz compression.
func (Xz) OpenReader ¶
func (Xz) OpenWriter ¶
type Zip ¶
type Zip struct { // Only compress files which are not already in a // compressed format (determined simply by examining // file extension). SelectiveCompression bool // The method or algorithm for compressing stored files. Compression uint16 // If true, errors encountered during reading or writing // a file within an archive will be logged and the // operation will continue on remaining files. ContinueOnError bool // For files in zip archives that do not have UTF-8 // encoded filenames and comments, specify the character // encoding here. TextEncoding string }
func (Zip) ArchiveAsync ¶
func (Zip) Extract ¶
func (z Zip) Extract(ctx context.Context, sourceArchive io.Reader, pathsInArchive []string, handleFile FileHandler) error
Extract extracts files from z, implementing the Extractor interface. Uniquely, however, sourceArchive must be an io.ReaderAt and io.Seeker, which are oddly disjoint interfaces from io.Reader which is what the method signature requires. We chose this signature for the interface because we figure you can Read() from anything you can ReadAt() or Seek() with. Due to the nature of the zip archive format, if sourceArchive is not an io.Seeker and io.ReaderAt, an error is returned.
type Zlib ¶
type Zlib struct {
CompressionLevel int
}
Zlib facilitates zlib compression.
func (Zlib) OpenReader ¶
func (Zlib) OpenWriter ¶
type Zstd ¶
Zstd facilitates Zstandard compression.