Documentation
¶
Overview ¶
Package archive is a convenience package for enumerating the contents of zip files, tar files, and compressed tar files. Supported archive types are: zip, tar, gzip-compressed tar, bzip2-compressed tar, and xz-compressed tar.
Usage ¶
To list the contents of a zip file:
func zipCallback(file *zip.File) error { if file.FileInfo().IsDir() { fmt.Printf("Dir : %s\n", file.Name) } else { fmt.Printf("File: %s\n", file.Name) } return nil } func main() { err := archive.WalkZip("test.zip", zipCallback) if err != nil { log.Fatal(err) } }
To extract the contents of a .tar.xz file:
func tarCallback(reader *tar.Reader, header *tar.Header) error { if header.FileInfo().IsDir() { os.MkdirAll(header.Name, 0700) return nil } fo, err := os.Create(header.Name) if err != nil { return err } defer fo.Close() _, err := io.Copy(fo, reader) if err != nil { return err } return nil } func main() { err := archive.WalkTarXz("test.tar.xz", tarCallback) if err != nil { log.Fatal(err) } }
To determine the type of archive file:
func main() { archiveType, err := archive.DetermineType(archiveFilename) if err != nil { fmt.Fprintln(os.Stderr, "Unable to determine the file's archive type.") os.Exit(1) } switch archiveType { case archive.Tar: err = archive.WalkTar(archiveFilename, tarCallback) case archive.TarBz2: err = archive.WalkTarBzip2(archiveFilename, tarCallback) case archive.TarGz: err = archive.WalkTarGz(archiveFilename, tarCallback) case archive.TarXz: err = archive.WalkTarXz(archiveFilename, tarCallback) case archive.Zip: err = archive.WalkZip(archiveFilename, zipCallback) } if err != nil { log.Fatal(err) } }
Index ¶
- func WalkTar(archivePath string, callback TarCallback) error
- func WalkTarBzip2(archivePath string, callback TarCallback) error
- func WalkTarGz(archivePath string, callback TarCallback) error
- func WalkTarXz(archivePath string, callback TarCallback) error
- func WalkZip(archivePath string, callback ZipCallback) error
- type TarCallback
- type Type
- type ZipCallback
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WalkTar ¶
func WalkTar(archivePath string, callback TarCallback) error
WalkTar walks the contents of a tar file and invokes the callback function for each entry.
Example ¶
callback := func(reader *tar.Reader, header *tar.Header) error { fmt.Printf("%s\n", header.Name) return nil } err := WalkTar("testdata/sample.tar", callback) if err != nil { log.Fatal(err) }
Output: sample/text/lorem.txt sample/text/ sample/
func WalkTarBzip2 ¶
func WalkTarBzip2(archivePath string, callback TarCallback) error
WalkTarBzip2 walks the contents of a bzip2-compressed tar file and invokes the callback function for each entry.
Example ¶
callback := func(reader *tar.Reader, header *tar.Header) error { fmt.Printf("%s\n", header.Name) return nil } err := WalkTarBzip2("testdata/sample.tar.bz2", callback) if err != nil { log.Fatal(err) }
Output: sample/ sample/text/ sample/text/lorem.txt
func WalkTarGz ¶
func WalkTarGz(archivePath string, callback TarCallback) error
WalkTarGz walks the contents of a gzip-compressed tar file and invokes the callback function for each entry.
Example ¶
callback := func(reader *tar.Reader, header *tar.Header) error { fmt.Printf("%s\n", header.Name) return nil } err := WalkTarGz("testdata/sample.tar.gz", callback) if err != nil { log.Fatal(err) }
Output: sample/ sample/text/ sample/text/lorem.txt
func WalkTarXz ¶
func WalkTarXz(archivePath string, callback TarCallback) error
WalkTarXz walks the contents of a lzma2-compressed (xz) tar file and invokes the callback function for each entry.
Example ¶
callback := func(reader *tar.Reader, header *tar.Header) error { fmt.Printf("%s\n", header.Name) return nil } err := WalkTarXz("testdata/sample.tar.xz", callback) if err != nil { log.Fatal(err) }
Output: sample/ sample/text/ sample/text/lorem.txt
func WalkZip ¶
func WalkZip(archivePath string, callback ZipCallback) error
WalkZip walks the contents of a zip file and invokes the callback function for each entry.
Example ¶
callback := func(file *zip.File) error { fmt.Printf("%s\n", file.Name) return nil } err := WalkZip("testdata/sample.zip", callback) if err != nil { log.Fatal(err) }
Output: sample/ sample/text/ sample/text/lorem.txt
Types ¶
type TarCallback ¶
TarCallback is the type of function called for each file or directory entry visited by the WalkTar functions.
type Type ¶
type Type uint
Type defines the archive types that can be processed in this package.
func DetermineType ¶
DetermineType identifies the archive file type based on the extensions present in the filename. Files with the ".tar" extension will be identified as Tar. Files with ".tar.bz2", ".tar.bzip2", ".tbz", or ".tbz2" extensions will be identified as TarBz2. Files with ".tar.gz" or ".tgz" extensions will be identified as TarGz. Files with ".tar.xz" or ".txz" extensions will be identified as TarXz. Files with the ".zip" extension will be identified as Zip. Anything else returns 0 and a non-nil error.
Example ¶
filename := "sample.tar.gz" typ, err := DetermineType(filename) if err != nil { log.Fatal(err) } fmt.Printf("File %s is type %s.", filename, typ)
Output: File sample.tar.gz is type TarGz.
type ZipCallback ¶
ZipCallback is the type of function called for each file or directory entry visited by WalkZip.