tar

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2021 License: MIT Imports: 8 Imported by: 1

README

tar

PkgGoDev Build Status codecov Go Report Card LICENSE

Package tar implements access to tar archives.

Feature

  • Tar
  • Gzip
  • Tar files or dirs

Get started

Install
go get github.com/hslam/tar
Import
import "github.com/hslam/tar"
Usage
Example
package main

import (
	"fmt"
	"github.com/hslam/tar"
	"os"
)

func main() {
	name := "file"
	targz := "file.tar.gz"
	defer os.Remove(name)
	defer os.Remove(targz)
	file, err := os.Create(name)
	if err != nil {
		panic(err)
	}
	contents := "Hello World"
	file.Write([]byte(contents))
	file.Close()
	tar.Targz(targz, name)
	os.Remove(name)
	tar.Untargz(targz)
	f, err := os.Open(name)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	buf := make([]byte, len(contents))
	f.Read(buf)
	fmt.Println(string(buf))
}
Tar bytes example
package main

import (
	"fmt"
	"github.com/hslam/tar"
	"os"
)

func main() {
	targz := "file.tar.gz"
	defer os.Remove(targz)
	tw, err := tar.NewGzipFileWriter(targz)
	if err != nil {
		panic(err)
	}
	tw.TarBytes("file", []byte("Hello World"))
	tw.Flush()
	tw.Close()
	tr, err := tar.NewGzipFileReader(targz)
	if err != nil {
		panic(err)
	}
	_, _, data, err := tr.NextBytes()
	if err != nil {
		panic(err)
	}
	fmt.Println(string(data))
}
Output
Hello World
License

This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)

Author

tar was written by Meng Huang.

Documentation

Overview

Package tar implements access to tar archives.

Index

Constants

View Source
const TarSuffix = ".tar"

TarSuffix represents the tar suffix.

View Source
const TargzSuffix = ".tar.gz"

TargzSuffix represents the tar gzip suffix.

Variables

View Source
var ErrTooManyArgs = errors.New("too many arguments")

ErrTooManyArgs is returned when the arguments length is bigger than 1.

Functions

func Tar

func Tar(tar string, paths ...string) error

Tar tars all the paths to the tar file.

func Targz

func Targz(targz string, paths ...string) error

Targz tars all the paths to the tar gzip file.

func Untar

func Untar(tar string, dir ...string) ([]string, []string, error)

Untar untars all the files to dir.

func Untargz

func Untargz(targz string, dir ...string) ([]string, []string, error)

Untargz untars all the files to dir.

Types

type Reader

type Reader struct {
	*tar.Reader
	// contains filtered or unexported fields
}

Reader provides sequential access to the contents of a tar archive.

func NewFileReader

func NewFileReader(name string) (*Reader, error)

NewFileReader creates a new Reader reading from file.

func NewGzipFileReader

func NewGzipFileReader(name string) (r *Reader, err error)

NewGzipFileReader creates a new gzip Reader reading from file.

func NewGzipReader

func NewGzipReader(r io.Reader) (*Reader, error)

NewGzipReader creates a new gzip Reader reading from r.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader creates a new Reader reading from r.

func (*Reader) Close

func (t *Reader) Close() error

Close closes the file.

func (*Reader) NextBytes

func (t *Reader) NextBytes() (name string, isDir bool, data []byte, err error)

NextBytes advances to the next file name and bytes in the tar archive.

func (*Reader) NextFile

func (t *Reader) NextFile(dir ...string) (name string, isDir bool, err error)

NextFile advances to the next file in the tar archive.

func (*Reader) Untar

func (t *Reader) Untar(dir ...string) (files, dirs []string, err error)

Untar untars all the files to dir.

type Writer

type Writer struct {
	*tar.Writer
	// contains filtered or unexported fields
}

Writer provides sequential writing of a tar archive.

func NewFileWriter

func NewFileWriter(name string) (*Writer, error)

NewFileWriter creates a new Writer writing to file.

func NewGzipFileWriter

func NewGzipFileWriter(name string) (*Writer, error)

NewGzipFileWriter creates a new gzip Writer writing to file.

func NewGzipWriter

func NewGzipWriter(w io.Writer) *Writer

NewGzipWriter creates a new gzip Writer writing to w.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new Writer writing to w.

func (*Writer) Close

func (w *Writer) Close() error

Close closes the tar archive by flushing the padding, and writing the footer. If the current file (from a prior call to WriteHeader) is not fully written, then this returns an error.

func (*Writer) Flush

func (w *Writer) Flush() error

Flush finishes writing the current file's block padding. The current file must be fully written before Flush can be called.

This is unnecessary as the next call to WriteHeader or Close will implicitly flush out the file's padding.

func (*Writer) Tar

func (w *Writer) Tar(paths ...string) error

Tar tars all the paths to the tar file.

func (*Writer) TarBytes

func (w *Writer) TarBytes(name string, data []byte) (err error)

TarBytes tars a file with the file name and data.

func (*Writer) TarDir

func (w *Writer) TarDir(dir string) error

TarDir tars a dir to the tar file.

func (*Writer) TarFile

func (w *Writer) TarFile(name string) error

TarFile writes a file to the tar file.

Jump to

Keyboard shortcuts

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