targz

package
v0.0.0-...-e53137b Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2020 License: BSD-3-Clause Imports: 5 Imported by: 6

Documentation

Overview

Example
package main

import (
	"archive/tar"
	"bytes"
	"fmt"
	"github.com/debber/debber-v0.3/targz"
	"io"
	"log"
	"os"
)

func main() {
	// Create a buffer to write our archive to.
	wtr := new(bytes.Buffer)

	// 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)
	}
	// Open the ar archive for reading.
	rdr := bytes.NewReader(wtr.Bytes())

	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()
	}

}
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