1
0
Fork 1
mirror of https://github.com/vbatts/tar-split.git synced 2024-11-26 01:35:39 +00:00
tar-split/main.go

72 lines
1.3 KiB
Go
Raw Normal View History

package main
import (
"flag"
"fmt"
"io"
"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()
2015-02-19 23:07:22 +00:00
output, err := os.Create(fmt.Sprintf("%s.out", arg))
if err != nil {
log.Fatal(err)
}
defer output.Close()
log.Printf("writing %q to %q", fh.Name(), output.Name())
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()
2015-02-19 23:07:22 +00:00
output.Write(pre)
var i int64
2015-02-19 23:07:22 +00:00
if i, err = io.Copy(output, tr); err != nil {
log.Println(err)
break
}
post := tr.RawBytes()
2015-02-19 23:07:22 +00:00
output.Write(pre)
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)
2015-02-19 23:07:22 +00:00
fmt.Printf("Compare like `cmp -bl %s %s | less`\n", fh.Name(), output.Name())
} else {
fmt.Printf("Size: %d; Sum: %d\n", size, sum)
}
}()
}
}