Go Home Page
The Go Programming Language

Package tar

import "archive/tar"

The tar package implements access to tar archives. It aims to cover most of the variations, including those produced by GNU and BSD tars.

References:

http://www.freebsd.org/cgi/man.cgi?query=tar&sektion=5
http://www.gnu.org/software/tar/manual/html_node/Standard.html

Package files

common.go reader.go writer.go

Constants

const (


    // Types
    TypeReg           = '0'
    TypeRegA          = '\x00'
    TypeLink          = '1'
    TypeSymlink       = '2'
    TypeChar          = '3'
    TypeBlock         = '4'
    TypeDir           = '5'
    TypeFifo          = '6'
    TypeCont          = '7'
    TypeXHeader       = 'x'
    TypeXGlobalHeader = 'g'
)

Variables

var (
    ErrWriteTooLong    = os.NewError("write too long")
    ErrFieldTooLong    = os.NewError("header field too long")
    ErrWriteAfterClose = os.NewError("write after close")
)
var (
    HeaderError os.Error = os.ErrorString("invalid tar header")
)

A Header represents a single header in a tar archive. Some fields may not be populated.

type Header struct {
    Name     string
    Mode     int64
    Uid      int64
    Gid      int64
    Size     int64
    Mtime    int64
    Typeflag byte
    Linkname string
    Uname    string
    Gname    string
    Devmajor int64
    Devminor int64
    Atime    int64
    Ctime    int64
}

type Reader

A Reader provides sequential access to the contents of a tar archive. A tar archive consists of a sequence of files. The Next method advances to the next file in the archive (including the first), and then it can be treated as an io.Reader to access the file's data.

Example:

tr := tar.NewReader(r);
for {
	hdr, err := tr.Next();
	if err != nil {
		// handle error
	}
	if hdr == nil {
		// end of tar archive
		break
	}
	io.Copy(data, tr);
}

type Reader struct {
    // contains unexported fields
}

func NewReader

func NewReader(r io.Reader) *Reader

NewReader creates a new Reader reading from r.

func (*Reader) Next

func (tr *Reader) Next() (*Header, os.Error)

Next advances to the next entry in the tar archive.

func (*Reader) Read

func (tr *Reader) Read(b []byte) (n int, err os.Error)

Read reads from the current entry in the tar archive. It returns 0, os.EOF when it reaches the end of that entry, until Next is called to advance to the next entry.

type Writer

A Writer provides sequential writing of a tar archive in POSIX.1 format. A tar archive consists of a sequence of files. Call WriteHeader to begin a new file, and then call Write to supply that file's data, writing at most hdr.Size bytes in total.

Example:

tw := tar.NewWriter(w);
hdr := new(Header);
hdr.Size = length of data in bytes;
// populate other hdr fields as desired
if err := tw.WriteHeader(hdr); err != nil {
	// handle error
}
io.Copy(tw, data);
tw.Close();

type Writer struct {
    // contains unexported fields
}

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new Writer writing to w.

func (*Writer) Close

func (tw *Writer) Close() os.Error

Close closes the tar archive, flushing any unwritten data to the underlying writer.

func (*Writer) Flush

func (tw *Writer) Flush() os.Error

Flush finishes writing the current file (optional).

func (*Writer) Write

func (tw *Writer) Write(b []byte) (n int, err os.Error)

Write writes to the current entry in the tar archive. Write returns the error ErrWriteTooLong if more than hdr.Size bytes are written after WriteHeader.

func (*Writer) WriteHeader

func (tw *Writer) WriteHeader(hdr *Header) os.Error

WriteHeader writes hdr and prepares to accept the file's contents. WriteHeader calls Flush if it is not the first header. Calling after a Close will return ErrWriteAfterClose.