Skip to content

Instantly share code, notes, and snippets.

@sdomino
Last active May 14, 2020 12:38
Show Gist options
  • Save sdomino/e6bc0c98f87843bc26bb to your computer and use it in GitHub Desktop.
Save sdomino/e6bc0c98f87843bc26bb to your computer and use it in GitHub Desktop.
// Tar takes a source and variable writers and walks 'source' writing each file
// found to the tar writer; the purpose for accepting multiple writers is to allow
// for multiple outputs (for example a file, or md5 hash)
func Tar(src string, writers ...io.Writer) error {
// ensure the src actually exists before trying to tar it
if _, err := os.Stat(src); err != nil {
return fmt.Errorf("Unable to tar files - %v", err.Error())
}
mw := io.MultiWriter(writers...)
gzw := gzip.NewWriter(mw)
defer gzw.Close()
tw := tar.NewWriter(gzw)
defer tw.Close()
// walk path
return filepath.Walk(src, func(file string, fi os.FileInfo, err error) error {
// return on any error
if err != nil {
return err
}
// return on non-regular files (thanks to [kumo](https://medium.com/@komuw/just-like-you-did-fbdd7df829d3) for this suggested update)
if !fi.Mode().IsRegular() {
return nil
}
// create a new dir/file header
header, err := tar.FileInfoHeader(fi, fi.Name())
if err != nil {
return err
}
// update the name to correctly reflect the desired destination when untaring
header.Name = strings.TrimPrefix(strings.Replace(file, src, "", -1), string(filepath.Separator))
// write the header
if err := tw.WriteHeader(header); err != nil {
return err
}
// open files for taring
f, err := os.Open(file)
if err != nil {
return err
}
// copy file data into tar writer
if _, err := io.Copy(tw, f); err != nil {
return err
}
// manually close here after each file operation; defering would cause each file close
// to wait until all operations have completed.
f.Close()
return nil
})
}
@thoslin
Copy link

thoslin commented May 16, 2018

line 42. "info" should be "fi"

@littlecxm
Copy link

@thoslin or define by info, err := os.Stat(src) ?

@ahmetb
Copy link

ahmetb commented Aug 1, 2019

When you do tw.Close(), you should definitely read the returned error as it may indicate errors about tar-ing up files. I recommend moving it after filepath.Walk() and definitely capturing error from tw.Close() if you want to know tarballing is complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment