Created
February 1, 2018 05:48
-
-
Save mostlygeek/d54f10c322e3efdffb819d3128d6b966 to your computer and use it in GitHub Desktop.
list files from a tar archive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// tweaked from | |
// original source: https://gist.github.com/indraniel/1a91458984179ab4cf80 | |
import ( | |
"archive/tar" | |
"compress/gzip" | |
"flag" | |
"fmt" | |
"io" | |
"os" | |
) | |
func main() { | |
flag.Parse() | |
sourceFile := flag.Arg(0) | |
if sourceFile == "" { | |
fmt.Println("Dude, you didn't pass in a tar file!") | |
os.Exit(1) | |
} | |
processFile(sourceFile) | |
} | |
func processFile(srcFile string) { | |
f, err := os.Open(srcFile) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
defer f.Close() | |
var source io.Reader | |
// make a gzip reader | |
gzf, err := gzip.NewReader(f) | |
if err != nil { | |
fmt.Println("Doesn't look like it is compressed...pretending it is a raw tar") | |
source = f | |
} else { | |
source = gzf | |
} | |
// make a tar reader | |
tarReader := tar.NewReader(source) | |
for { | |
header, err := tarReader.Next() | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
switch header.Typeflag { | |
case tar.TypeDir: | |
continue | |
case tar.TypeReg: | |
fmt.Println("File:", header.Name) | |
default: | |
fmt.Printf("%s : %c %s %s\n", | |
"Yikes! Unable to figure out type", | |
header.Typeflag, | |
"in file", | |
header.Name, | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The point was to process the tar.gz while it was uploading. You don't have to wait for the whole file, write it to disk and then decompress it. The file structure of tar.gz files allow you to process them as a stream.