Documentation ¶
Overview ¶
Example ¶
package main import ( "archive/tar" "bytes" "fmt" "io" "log" "os" "path/filepath" "github.com/mwpcheung/debgo/targz" ) // 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 ¶
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.
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 ¶
NewWriter is a factory for Writer. It wraps the io.Writer with a Tar writer and Gzip writer
func NewWriterFromFile ¶
NewWriterFromFile is a factory for Writer
Click to show internal directories.
Click to hide internal directories.