Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var EOA error = errors.New("end of archive")
EOA signifies that the archive has been exhausted.
Functions ¶
func DirReader ¶
DirReader returns a specialized reader that reads all the files in a directory in a tar archive as if they were one file.
In this sense, it is very similar to the reader returned by io.MultiReader.
The reader advances the header of tar forward as long as it reads from files in a given directory; this is modified on the callers side, hence the need to pass a pointer to a pointer of tar.Header. When Read then returns io.EOF, the current header is on the next valid file. tr.Next() must not be called after reading from DirReader.
DirReader differentiates between the end of directory and end of archive by returning io.EOF in the first and EOA in the latter case.
BUG: at the moment it chokes if there is another directory in the directory given.
Example ¶
package main import ( "archive/tar" "bufio" "fmt" "io" "log" "os" "github.com/goulash/archive" ) func main() { file, err := os.Open("testdata/dir_reader_data.tar") if err != nil { die(err) } defer file.Close() tr := tar.NewReader(file) hdr, err := tr.Next() for hdr != nil { fi := hdr.FileInfo() if !fi.IsDir() { fmt.Fprintf(os.Stderr, "error: unexpected file '%s'\n", hdr.Name) hdr, err = tr.Next() if err != nil { if err == io.EOF { break } die(err) } continue } fmt.Println(hdr.Name) r := archive.DirReader(tr, &hdr) err = printPrefixed(r, "\t") if err != nil { if err == archive.EOA { break } die(err) } } } func printPrefixed(r io.Reader, prefix string) error { br := bufio.NewReader(r) for { line, err := br.ReadString('\n') if err != nil { if err == io.EOF { break } return err } fmt.Printf("%s%s", prefix, line) } return nil } func die(err error) { log.Fatalf("error: %s\n", err) }
Output: dir1/ dir1/file2 content dir1/file1 content dir2/ dir2/file3 this sentence should span three files. dir2/file2 and which will appear in betwwen the three files, dir2/file1 Apart from the header which is written in each file,
func ReadFileFromArchive ¶
ReadFileFromArchive tries to read the file specified from the (compressed) archive. Archive formats supported are:
.tar .tar.gz .tar.bz2 .tar.xz .tar.zst
Types ¶
type Decompressor ¶
type Decompressor struct {
// contains filtered or unexported fields
}
Decompressor is a universal decompressor that, given a filepath, chooses the appropriate decompression algorithm.
At the moment, only the gzip, bzip2, and lzma (as in ".xz") are supported. The decompressor needs to be closed after usage.
func NewDecompressor ¶
func NewDecompressor(filepath string) (*Decompressor, error)
NewDecompressor creates a new decompressor based on the file extension of the given file. The returned Decompressor can be Read and Closed.
func (*Decompressor) Close ¶
func (d *Decompressor) Close() error