Go library for extracting archives (tar, zip, etc.)
tar
tar.xz
- xztar.bz2
- bzip2tar.gz
- gziptar.zst
- zstd
zip
For complete documentation and examples, see our pkg.go.dev documentation.
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
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).
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 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.
LGPL-3.0