Created
February 23, 2015 19:05
-
-
Save indraniel/1a91458984179ab4cf80 to your computer and use it in GitHub Desktop.
Reading through a tar.gz file in Go / golang
This file contains 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 | |
import ( | |
"archive/tar" | |
"compress/gzip" | |
"flag" | |
"fmt" | |
"io" | |
"os" | |
) | |
func main() { | |
// get the arguments from the command line | |
numPtr := flag.Int("n", 4, "an integer") | |
flag.Parse() | |
sourceFile := flag.Arg(0) | |
if sourceFile == "" { | |
fmt.Println("Dude, you didn't pass in a tar file!") | |
os.Exit(1) | |
} | |
fmt.Println("arg 1: ", flag.Arg(0)) | |
processFile(sourceFile, *numPtr) | |
} | |
func processFile(srcFile string, num int) { | |
f, err := os.Open(srcFile) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
defer f.Close() | |
gzf, err := gzip.NewReader(f) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
tarReader := tar.NewReader(gzf) | |
i := 0 | |
for { | |
header, err := tarReader.Next() | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
name := header.Name | |
switch header.Typeflag { | |
case tar.TypeDir: | |
continue | |
case tar.TypeReg: | |
fmt.Println("(", i, ")", "Name: ", name) | |
if i == num { | |
fmt.Println(" --- ") | |
io.Copy(os.Stdout, tarReader) | |
fmt.Println(" --- ") | |
os.Exit(0) | |
} | |
default: | |
fmt.Printf("%s : %c %s %s\n", | |
"Yikes! Unable to figure out type", | |
header.Typeflag, | |
"in file", | |
name, | |
) | |
} | |
i++ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I echo @joonas-fi, thanks for the improvements @dkartachov !