ar

package module
v0.0.0-...-282caa8 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2015 License: BSD-3-Clause Imports: 4 Imported by: 53

README

Package ar implements reading and writing of ar archives.
It supports reading archives in the GNU and BSD formats, but
only supports writing in the BSD format.

Documentation

Overview

Package ar implements reading and writing of ar archives. It supports reading archives in the GNU and BSD formats, but only supports writing in the BSD format.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingGlobalHeader = errors.New("ar: missing global header")
	ErrFileHeader          = errors.New("ar: invalid ar file header")
	ErrWriteAfterClose     = errors.New("ar: write after close")
	ErrWriteTooLong        = errors.New("ar: write too long")
)

Functions

This section is empty.

Types

type Header struct {
	Name  string
	Mode  int64
	Uid   int
	Gid   int
	Size  int64
	Mtime int64
}

A Header represents a single file header in an ar archive.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

A Reader provides sequential access to the contents of a BSD or GNU-style ar archive. An archive file consists of a sequence of files. The Next method advances to the next file in the archive (including the first). After Next has returned a header, the Reader can be treated as an io.Reader to access the data of the file described by the header received from Next.

Example:

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

func NewReader

func NewReader(r io.Reader) *Reader

NewReader creates a new Reader reading from r.

func (*Reader) Next

func (ar *Reader) Next() (hdr *Header, err error)

Next advances to the next entry in the archive.

func (*Reader) Read

func (ar *Reader) Read(b []byte) (n int, err error)

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

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

A Writer provides sequential writing of an ar archive in BSD format. It does not support writing in the GNU format, since the GNU-style extended filenames cannot be written sequentially. The BSD ar format is widely compatible with most modern ar readers out there.

An ar 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:

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

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new Writer writing to w.

func (*Writer) Close

func (aw *Writer) Close() error

Closes the ar achive, flushing any unwritten data to the underlying writer.

func (*Writer) Flush

func (aw *Writer) Flush() error

Flush finishes writing the current file (optional).

func (*Writer) Write

func (aw *Writer) Write(b []byte) (n int, err error)

Write writes the current entry in the ar archive. Write returns the error ErrWriteTooLong if more than hdr.Size bytes are written following a call to WriteHeader.

func (*Writer) WriteHeader

func (aw *Writer) WriteHeader(hdr *Header) (err error)

WriteHeader writes hdr and prepares to accept the file's content. WriteHeader calls Flush to correctly pad the last written file. Calling after WriteHeader a Close will return ErrWriteAfterClose.

Jump to

Keyboard shortcuts

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