Package archive
is a convenience package for walking/enumerating the contents of zip files, tar files, and compressed tar files through callback functions. Supported archive types include: zip, tar, gzip-compressed tar, bzip2-compressed tar, and xz-compressed tar.
go get github.com/kristinjeanna/archive
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)
}
}
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)
}
}
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)
}
}
- XZ compression support via github.com/ulikunitz/xz