Created
February 17, 2014 22:29
-
-
Save jonmorehouse/9060515 to your computer and use it in GitHub Desktop.
Quick golang script for creating a gzipped tarball
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 ( | |
"os" | |
"archive/tar" | |
"log" | |
"io" | |
"compress/gzip" | |
) | |
func addFile(tw * tar.Writer, path string) error { | |
file, err := os.Open(path) | |
if err != nil { | |
return err | |
} | |
defer file.Close() | |
if stat, err := file.Stat(); err == nil { | |
// now lets create the header as needed for this file within the tarball | |
header := new(tar.Header) | |
header.Name = path | |
header.Size = stat.Size() | |
header.Mode = int64(stat.Mode()) | |
header.ModTime = stat.ModTime() | |
// write the header to the tarball archive | |
if err := tw.WriteHeader(header); err != nil { | |
return err | |
} | |
// copy the file data to the tarball | |
if _, err := io.Copy(tw, file); err != nil { | |
return err | |
} | |
} | |
return nil | |
} | |
func main() { | |
// set up the output file | |
file, err := os.Create("output.tar.gz") | |
if err != nil { | |
log.Fatalln(err) | |
} | |
defer file.Close() | |
// set up the gzip writer | |
gw := gzip.NewWriter(file) | |
defer gw.Close() | |
tw := tar.NewWriter(gw) | |
defer tw.Close() | |
// grab the paths that need to be added in | |
paths := []string{ | |
"readme.txt", | |
} | |
// add each file as needed into the current tar archive | |
for i := range paths { | |
if err := addFile(tw, paths[i]); err != nil { | |
log.Fatalln(err) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI, there's also tar.FileInfoHeader which makes the tar.Header for you.