Skip to content

Instantly share code, notes, and snippets.

@icub3d
Forked from sdomino/publish.go
Created October 2, 2015 02:14
Show Gist options
  • Save icub3d/aa86bc52d90203504760 to your computer and use it in GitHub Desktop.
Save icub3d/aa86bc52d90203504760 to your computer and use it in GitHub Desktop.
package main
import (
"archive/tar"
"compress/gzip"
"crypto/md5"
"fmt"
"io"
"log"
"os"
"path/filepath"
"github.com/icub3d/gop/wrapio"
)
func main() {
out, err := os.Create(os.Args[1])
if err != nil {
log.Fatalln("opening", os.Args[1], err)
}
defer out.Close()
h := md5.New()
hw := wrapio.NewHashWriter(h, out)
gzw := gzip.NewWriter(hw)
tw := tar.NewWriter(gzw)
for _, path := range os.Args[2:] {
err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if info.IsDir() { // skip directories.
return nil
}
// Open up the file
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
// Make the header and write it to the tarball.
header := &tar.Header{
Name: path,
Size: info.Size(),
Mode: int64(info.Mode()),
ModTime: info.ModTime(),
}
if err := tw.WriteHeader(header); err != nil {
return err
}
// copy the file data to the tarball
if _, err := io.Copy(tw, f); err != nil {
return err
}
return nil
})
if err != nil {
log.Println("walking:", err)
}
}
tw.Close()
gzw.Close()
fmt.Printf("%x\n", h.Sum(nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment