extract

package module
Version: v3.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2021 License: MIT Imports: 16 Imported by: 0

README

Extract

Build Status GitHub license Godoc Reference

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

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 buffer and a destination:

data, _ := ioutil.ReadFile("path/to/file.tar.bz2")
buffer := bytes.NewBuffer(data)
extract.Bz2(data, "/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(data, "/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(data, "/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
}

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(data, "path/where/to/extract", nil)

Documentation

Overview

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 buffer and a destination:

data, _ := ioutil.ReadFile("path/to/file.tar.bz2")
buffer := bytes.NewBuffer(data)
extract.TarBz2(data, "/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.TarBz2(data, "/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(data, "/path/where/to/extract", nil)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Archive

func Archive(ctx context.Context, body io.Reader, location string, rename Renamer) error

Archive extracts a generic archived stream of data in the specified location. It automatically detects the archive type and accepts a rename function to handle the names of the files. If the file is not an archive, an error is returned.

func Bz2

func Bz2(ctx context.Context, body io.Reader, location string, rename Renamer) error

Bz2 extracts a .bz2 or .tar.bz2 archived stream of data in the specified location. It accepts a rename function to handle the names of the files (see the example)

func Gz

func Gz(ctx context.Context, body io.Reader, location string, rename Renamer) error

Gz extracts a .gz or .tar.gz archived stream of data in the specified location. It accepts a rename function to handle the names of the files (see the example)

func Tar

func Tar(ctx context.Context, body io.Reader, location string, rename Renamer) error

Tar extracts a .tar archived stream of data in the specified location. It accepts a rename function to handle the names of the files (see the example)

func Xz

func Xz(ctx context.Context, body io.Reader, location string, rename Renamer) error

Xz extracts a .xz or .tar.xz archived stream of data in the specified location. It accepts a rename function to handle the names of the files (see the example)

func Zip

func Zip(ctx context.Context, body io.Reader, location string, rename Renamer) error

Zip extracts a .zip archived stream of data in the specified location. It accepts a rename function to handle the names of the files (see the example).

Types

type Extractor

type Extractor struct {
	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
	}
}

Extractor is more sophisticated than the base functions. It allows to write over an interface rather than directly on the filesystem

func (*Extractor) Archive

func (e *Extractor) Archive(ctx context.Context, body io.Reader, location string, rename Renamer) error

Archive extracts a generic archived stream of data in the specified location. It automatically detects the archive type and accepts a rename function to handle the names of the files. If the file is not an archive, an error is returned.

func (*Extractor) Bz2

func (e *Extractor) Bz2(ctx context.Context, body io.Reader, location string, rename Renamer) error

Bz2 extracts a .bz2 or .tar.bz2 archived stream of data in the specified location. It accepts a rename function to handle the names of the files (see the example)

func (*Extractor) Gz

func (e *Extractor) Gz(ctx context.Context, body io.Reader, location string, rename Renamer) error

Gz extracts a .gz or .tar.gz archived stream of data in the specified location. It accepts a rename function to handle the names of the files (see the example)

func (*Extractor) Tar

func (e *Extractor) Tar(ctx context.Context, body io.Reader, location string, rename Renamer) error

Tar extracts a .tar archived stream of data in the specified location. It accepts a rename function to handle the names of the files (see the example)

func (*Extractor) Xz

func (e *Extractor) Xz(ctx context.Context, body io.Reader, location string, rename Renamer) error

Zz extracts a .xz or .tar.xz archived stream of data in the specified location. It accepts a rename function to handle the names of the files (see the example)

func (*Extractor) Zip

func (e *Extractor) Zip(ctx context.Context, body io.Reader, location string, rename Renamer) error

Zip extracts a .zip archived stream of data in the specified location. It accepts a rename function to handle the names of the files (see the example).

type Renamer

type Renamer func(string) string

Renamer is a function that can be used to rename the files when you're extracting them. For example you may want to only extract files with a certain pattern. If you return an empty string they won't be extracted.

Directories

Path Synopsis
This utility is used to generate the archives used as testdata for zipslip vulnerability
This utility is used to generate the archives used as testdata for zipslip vulnerability

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL