Categorygithub.com/jaredallard/archives
modulepackage
1.0.0
Repository: https://github.com/jaredallard/archives.git
Documentation: pkg.go.dev

# README

archives

Latest Version License Github Workflow Status Codecov

Go library for extracting archives (tar, zip, etc.)

Supported Archive Types

  • tar
    • tar.xz - xz
    • tar.bz2 - bzip2
    • tar.gz - gzip
    • tar.zst - zstd
  • zip

Usage

For complete documentation and examples, see our pkg.go.dev documentation.

Extracting Archives

Extracting archives is simple with this package. Simply provide an io.Reader and the extension (which you can use archives.Ext to get!) and you're good to go.

resp, err := http.Get("https://getsamplefiles.com/download/zip/sample-1.zip")
if err != nil {}
defer resp.Body.Close()

err := archives.Extract(resp.Body, "dir-to-extract-into", &archives.ExtractOptions{
  Extension: archives.Ext("sample-1.zip"),
})
if err != nil {}

// Do something with the files in dir-to-extract-into

Picking a File out of an Archive

Sometimes you want to only grab a single file out of an archive. archives.Pick is helpful here.

resp, err := http.Get("https://getsamplefiles.com/download/zip/sample-1.zip")
if err != nil {}
defer resp.Body.Close()

a, err := archives.Open(resp.Body, &archives.OpenOptions{
  Extension: archives.Ext("sample-1.zip"),
})
if err != nil {}

// Pick a single file out of the zip archive
r, err := archives.Pick(a, archives.PickFilterByName("sample-1/sample-1.webp"))
if err != nil {}

// Do something with the returned [io.Reader] (r).

Working with Archives

You can also work with an archive directly, much like tar.Reader.

resp, err := http.Get("https://getsamplefiles.com/download/tar/sample-1.tar")
if err != nil {}
defer resp.Body.Close()

a, err := archives.Open(resp.Body, &archives.OpenOptions{
  Extension: archives.Ext("sample-1.tar"),
})
if err != nil {}

h, err := a.Next()
if err != nil {}

// Read the current file using `a` ([Archive]) which is an io.Reader,
// or only handle the `h` ([Header]). Your choice!

// Close out the archiver parser(s).
a.Close()

CGO

CGO is used for extracting xz archives by default. If you wish to not use CGO, simply set CGO_ENABLED to 0. This library will automatically use a pure-Go implementation instead.

License

LGPL-3.0

# Functions

Ext returns the extension of a file name based on the supported extensions in this package.
Extract extracts an archive to the provided destination.
Open opens an archive from the provided reader.
Pick returns an [io.Reader] that returns a specific file from the provided [Archive].
PickFilterByName returns a [PickFilterFn] that filters files by name.

# Constants

Contains the supported header types.
Contains the supported header types.

# Structs

ExtractOptions contains the options for extracting an archive.
Header represents metadata about a file in an archive.
OpenOptions contains the options for opening an archive.

# Interfaces

Archive represents an archive containing folders and files.
Archiver is an interface for interacting with creating [Archive]s from [io.Reader]s.

# Type aliases

HeaderType denotes a type of header.
PickFilterFn is a function that filters files in an archive.