From f45a3e13ee9f34a8098b0df97f512a6f2e072212 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 19 Feb 2015 16:50:30 -0500 Subject: [PATCH] main: example application to use the RawBytes() of 'archive/tar', and show the diff of the bytes read from the file itself. --- main.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..e54ef7e --- /dev/null +++ b/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "flag" + "fmt" + "io" + "io/ioutil" + "log" + "os" + + "./archive/tar" +) + +func main() { + flag.Parse() + log.SetOutput(os.Stderr) + for _, arg := range flag.Args() { + func() { + // Open the tar archive + fh, err := os.Open(arg) + if err != nil { + log.Fatal(err) + } + defer fh.Close() + + fi, err := fh.Stat() + if err != nil { + log.Fatal(err) + } + size := fi.Size() + var sum int64 + tr := tar.NewReader(fh) + tr.RawAccounting = true + for { + hdr, err := tr.Next() + if err != nil { + if err != io.EOF { + log.Println(err) + } + break + } + pre := tr.RawBytes() + var i int64 + if i, err = io.Copy(ioutil.Discard, tr); err != nil { + log.Println(err) + break + } + post := tr.RawBytes() + fmt.Println(hdr.Name, "pre:", len(pre), "read:", i, "post:", len(post)) + sum += int64(len(pre)) + sum += i + sum += int64(len(post)) + } + + if size != sum { + fmt.Printf("Size: %d; Sum: %d; Diff: %d\n", size, sum, size-sum) + } else { + fmt.Printf("Size: %d; Sum: %d\n", size, sum) + } + }() + } +}