targz

package
v0.0.0-...-b076d91 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2014 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Overview

Example
package main

import (
	"archive/tar"
	"bytes"
	"fmt"
	"github.com/laher/debgo-v0.2/targz"
	"io"
	"log"
	"os"
	"path/filepath"
)

// change this to true for generating an archive on the Filesystem
var (
	filename = filepath.Join("_test", "tmp.tar.gz")
	isFs     = true
)

func main() {
	// Create a buffer to write our archive to.
	wtr := writer(isFs)

	// Create a new ar archive.
	tgzw := targz.NewWriter(wtr)

	// Add some files to the archive.
	var files = []struct {
		Name, Body string
	}{
		{"readme.txt", "This archive contains some text files."},
		{"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
		{"todo.txt", "Get animal handling licence."},
	}
	for _, file := range files {
		hdr := &tar.Header{
			Name: file.Name,
			Size: int64(len(file.Body)),
		}
		if err := tgzw.WriteHeader(hdr); err != nil {
			log.Fatalln(err)
		}
		if _, err := tgzw.Write([]byte(file.Body)); err != nil {
			log.Fatalln(err)
		}
	}
	// Make sure to check the error on Close.
	if err := tgzw.Close(); err != nil {
		log.Fatalln(err)
	}
	rdr := reader(isFs, wtr)
	tgzr, err := targz.NewReader(rdr)
	if err != nil {
		log.Fatalln(err)
	}

	// Iterate through the files in the archive.
	for {
		hdr, err := tgzr.Next()
		if err == io.EOF {
			// end of ar archive
			break
		}
		if err != nil {
			log.Fatalln(err)
		}
		fmt.Printf("Contents of %s:\n", hdr.Name)
		if _, err := io.Copy(os.Stdout, tgzr); err != nil {
			log.Fatalln(err)
		}

		fmt.Println()
	}

}

func reader(isFs bool, w io.Writer) io.Reader {
	if isFs {
		fi := w.(*os.File)
		err := fi.Close()
		if err != nil {
			log.Fatalln(err)
		}

		r, err := os.Open(filename)
		if err != nil {
			log.Fatalln(err)
		}
		return r
	}
	buf := w.(*bytes.Buffer)
	// Open the ar archive for reading.
	r := bytes.NewReader(buf.Bytes())
	return r

}

func writer(isFs bool) io.Writer {
	if isFs {
		fi, err := os.Create(filename)
		if err != nil {
			log.Fatalln(err)
		}
		return fi
	}
	return new(bytes.Buffer)

}
Output:

Contents of readme.txt:
This archive contains some text files.
Contents of gopher.txt:
Gopher names:
George
Geoffrey
Gonzo
Contents of todo.txt:
Get animal handling licence.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	*tar.Reader
	WrappedReader io.Reader
	GzipReader    *gzip.Reader
}

A Reader provides sequential access to the contents of a tar.gz archive. A tar.gz 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.

func NewReader

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

NewReader creates a new Reader reading from r.

func (*Reader) Close

func (tgzr *Reader) Close() error

Close closes the gzip reader

type Writer

type Writer struct {
	*tar.Writer // Tar writer (wraps the GzipWriter)
	//Filename string       // Filename
	WrappedWriter io.Writer    // File writer
	GzipWriter    *gzip.Writer // Gzip writer (wraps the WrappedWriter)
}

Writer encapsulates the tar, gz and file operations of a .tar.gz file.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter is a factory for Writer. It wraps the io.Writer with a Tar writer and Gzip writer

func NewWriterFromFile

func NewWriterFromFile(archiveFilename string) (*Writer, error)

NewWriterFromFile is a factory for Writer

func (*Writer) Close

func (tgzw *Writer) Close() error

Close closes all 3 writers. Returns the first error

Jump to

Keyboard shortcuts

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