tarx

package module
v0.0.0-...-6e3da54 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2015 License: MIT Imports: 11 Imported by: 0

README

tarx

tarx is a golang package for archiving files and folders to tar format.

Installation
$ go get github.com/viniciuschiele/tarx
Examples

Compressing a file.

package main

import "github.com/viniciuschiele/tarx"

func main() {
    if err := tarx.Compress("example.tar", "example.txt", nil); err != nil {
        panic(err)
    }
}

Compressing a folder.

package main

import "github.com/viniciuschiele/tarx"

func main() {
    if err := tarx.Compress("example.tar", "example/folder", nil); err != nil {
      panic(err)
    }
}

Compression a folder with Gzip algorithm.

package main

import "github.com/viniciuschiele/tarx"

func main() {
    err := tarx.Compress("example.tar.gz", "example/folder", &tarx.CompressOptions{Compression: tarx.Gzip})
    if err != nil {
        panic(err)
    }
}

Keeping the last directory name in the path.

package main

import "github.com/viniciuschiele/tarx"

func main() {
    err := tarx.Compress("example.tar", "example/folder", &tarx.CompressOptions{IncludeSourceDir: true})
    if err != nil {
      return err
    }
}

Filtering files to be compressed.

package main

import "github.com/viniciuschiele/tarx"

func main() {
    filters := []string{"a.txt", "c/c2.txt"}
    err := tarx.Compress("example.tar", "example/folder", &tarx.CompressOptions{filters: filters})
    if eer != nil {
        panic(err)
    }
}

Extracting tar file into a directory.

package main

import "github.com/viniciuschiele/tarx"

func main() {
    if err := tarx.Extract("example.tar", "outputDir", nil}); err != nil {
        panic(err)
    }
}

Extracting tar file into a directory with filters.

package main

import "github.com/viniciuschiele/tarx"

func main() {
    filters := []string{"a.txt", "c/c2.txt"}
    err := tarx.Extract("example.tar", "outputDir", &tarx.ExtractOptions{Filters: filters})
    if err != nil {
        panic(err)
    }
}

Reading a specific file from the tar file.

package main

import "github.com/viniciuschiele/tarx"

func main() {
    header, reader, err := tarx.Find("example.tar", "a.txt")
    if err != nil {
        panic(err)
    }
}

Documentation

Overview

Package tarx implements access to tar archives on top of archive/​tar.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAppendNotSupported = errors.New("Append is only supported on compressed files")
	ErrBzip2NotSupported  = errors.New("Bzip2 is not supported for compression")
)

Common errors

Functions

func Compress

func Compress(fileName, srcPath string, options *CompressOptions) error

Compress compress a source path into a tar file. All files will be relative to the tar file.

func Extract

func Extract(fileName, targetDir string, options *ExtractOptions) error

Extract extracts the files from a tar file into a target directory.

func Find

func Find(fileName, targetFileName string) (*tar.Header, io.ReadCloser, error)

Find returns the header and ReadCloser for the entry in the tarfile that matches the filename. If nothing matches, an `os.ErrNotExists` error is returned. If the `targetFileName` is not a regular file it returns a reader `nil`.

func List

func List(fileName string) ([]*tar.Header, error)

List lists all entries from a tar file.

Types

type CompressOptions

type CompressOptions struct {
	Append           bool
	Compression      Compression
	IncludeSourceDir bool
	Filters          []string
}

CompressOptions is the compression configuration

type Compression

type Compression int

Compression is the state represents if compressed or not.

const (
	// Uncompressed represents the uncompressed.
	Uncompressed Compression = iota
	// Gzip is gzip compression algorithm.
	Gzip
	// Bzip2 is bzip2 compression algorithm.
	Bzip2
)

type ExtractOptions

type ExtractOptions struct {
	FlatDir    bool
	Filters    []string
	NoOverride bool
}

ExtractOptions is the decompression configuration

Jump to

Keyboard shortcuts

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