Skip to content

jaredallard/archives

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •