codeclysm/extract

A Go library to extract archives in zip, tar.gz or tar.bz2 formats

Go

50

81 commits

updated Aug 8, 2024

See the code

README

Extract

Build Status GitHub license Godoc Reference

import "github.com/codeclysm/extract/v4"

Package extract allows to extract archives in zip, tar.gz or tar.bz2 formats easily.

Most of the time you'll just need to call the proper function with a Reader and a destination:

file, _ := os.Open("path/to/file.tar.bz2")
extract.Bz2(context.TODO, file, "/path/where/to/extract", nil)

or also:

data, _ := ioutil.ReadFile("path/to/file.tar.bz2")
buffer := bytes.NewBuffer(data)
extract.Bz2(context.TODO, buffer, "/path/where/to/extract", nil)

Sometimes you'll want a bit more control over the files, such as extracting a subfolder of the archive. In this cases you can specify a renamer func that will change the path for every file:

var shift = func(path string) string {
    parts := strings.Split(path, string(filepath.Separator))
    parts = parts[1:]
    return strings.Join(parts, string(filepath.Separator))
}
extract.Bz2(context.TODO, file, "/path/where/to/extract", shift)

If you don't know which archive you're dealing with (life really is always a surprise) you can use Archive, which will infer the type of archive from the first bytes

extract.Archive(context.TODO, file, "/path/where/to/extract", nil)

If you need more control over how your files will be extracted you can use an Extractor.

It Needs a FS object that implements the FS interface:

type FS interface {
    Link(string, string) error
    MkdirAll(string, os.FileMode) error
    OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)
    Symlink(string, string) error
    Remove(path string) error
    Stat(name string) (os.FileInfo, error)
    Chmod(name string, mode os.FileMode) error
}

which contains only the required function to perform an extraction. This way it's easy to wrap os functions to chroot the path, or scramble the files, or send an event for each operation or even reimplementing them for an in-memory store, I don't know.

extractor := extract.Extractor{
    FS: fs,
}

extractor.Archive(context.TODO, file, "/path/where/to/extract", nil)
archive
bz2
extract
golang-library
tar
zip

codeclysm/extract

A Go library to extract archives in zip, tar.gz or tar.bz2 formats

Go

50

81 commits

updated Aug 8, 2024

See the code

README

Extract

Build Status GitHub license Godoc Reference

import "github.com/codeclysm/extract/v4"

Package extract allows to extract archives in zip, tar.gz or tar.bz2 formats easily.

Most of the time you'll just need to call the proper function with a Reader and a destination:

file, _ := os.Open("path/to/file.tar.bz2")
extract.Bz2(context.TODO, file, "/path/where/to/extract", nil)

or also:

data, _ := ioutil.ReadFile("path/to/file.tar.bz2")
buffer := bytes.NewBuffer(data)
extract.Bz2(context.TODO, buffer, "/path/where/to/extract", nil)

Sometimes you'll want a bit more control over the files, such as extracting a subfolder of the archive. In this cases you can specify a renamer func that will change the path for every file:

var shift = func(path string) string {
    parts := strings.Split(path, string(filepath.Separator))
    parts = parts[1:]
    return strings.Join(parts, string(filepath.Separator))
}
extract.Bz2(context.TODO, file, "/path/where/to/extract", shift)

If you don't know which archive you're dealing with (life really is always a surprise) you can use Archive, which will infer the type of archive from the first bytes

extract.Archive(context.TODO, file, "/path/where/to/extract", nil)

If you need more control over how your files will be extracted you can use an Extractor.

It Needs a FS object that implements the FS interface:

type FS interface {
    Link(string, string) error
    MkdirAll(string, os.FileMode) error
    OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)
    Symlink(string, string) error
    Remove(path string) error
    Stat(name string) (os.FileInfo, error)
    Chmod(name string, mode os.FileMode) error
}

which contains only the required function to perform an extraction. This way it's easy to wrap os functions to chroot the path, or scramble the files, or send an event for each operation or even reimplementing them for an in-memory store, I don't know.

extractor := extract.Extractor{
    FS: fs,
}

extractor.Archive(context.TODO, file, "/path/where/to/extract", nil)
archive
bz2
extract
golang-library
tar
zip

Project health

Overall

3.3/10

Maintained

0/10

Code review

5/10
OpenSSF Scorecard

Latest release

v2.2.0+incompatible · Apr 5, 2019

Releases this year

0
2 known vulnerabilities

Significant stargazers

Steve Francia

16,699 followers · starred Nov 2024

Federico Di Pierro

122 followers · starred Jan 2024

Jonas Heinrich

87 followers · starred Feb 2024