aboutsummaryrefslogtreecommitdiff
path: root/archive/archive.go
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2020-11-20 19:56:29 -0500
committerMitch Riedstra <mitch@riedstra.us>2020-11-20 19:56:29 -0500
commit971cc396e1d01f53f65a86278fd0ac4490565335 (patch)
tree19d0a1e861590b5058ca682c2f5a33828414cf3d /archive/archive.go
parent9ff47bdc17c70f87c78520d0636b2ff22918a408 (diff)
downloadsteam-export-971cc396e1d01f53f65a86278fd0ac4490565335.tar.gz
steam-export-971cc396e1d01f53f65a86278fd0ac4490565335.tar.xz
Reorganize. Update to use go modules.
Diffstat (limited to 'archive/archive.go')
-rw-r--r--archive/archive.go104
1 files changed, 104 insertions, 0 deletions
diff --git a/archive/archive.go b/archive/archive.go
new file mode 100644
index 0000000..639a864
--- /dev/null
+++ b/archive/archive.go
@@ -0,0 +1,104 @@
+package archive
+
+import (
+ "archive/tar"
+ "path/filepath"
+
+ "compress/gzip"
+
+ "io"
+ "os"
+
+ "strings"
+)
+
+type Archive struct {
+ Output string
+ Input []string
+ file *os.File
+}
+
+func (a *Archive) Tar(compressionType string) error {
+ var err error
+ if a.file, err = os.Create(a.Output); err != nil {
+ return err
+ }
+
+ defer a.file.Close()
+
+ var twriter *tar.Writer
+
+ // Set the compression type... if any
+ switch compressionType {
+ case "gz":
+ gzwriter, err := gzip.NewWriterLevel(a.file, gzip.BestSpeed)
+ if err != nil {
+ return err
+ }
+ defer gzwriter.Close()
+ // Write to the gzip writer
+ twriter = tar.NewWriter(gzwriter)
+ default:
+ // Write directly to the file
+ twriter = tar.NewWriter(a.file)
+ }
+
+ // Close off the tar writer when we're done
+ defer twriter.Close()
+
+ for _, v := range a.Input {
+ if err := filepath.Walk(v, tarWalkfn(twriter)); err != nil {
+ return err
+ }
+ }
+
+ return nil
+
+}
+
+func tarWalkfn(writer *tar.Writer) filepath.WalkFunc {
+ // This is an interesting trick to get around scoping issues
+ return func(path string, info os.FileInfo, err error) error {
+ if err != nil {
+ return err
+ }
+
+ if info.IsDir() {
+ return nil
+ }
+
+ f, err := os.Open(path)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+
+ // Convert Windows paths to Unix paths
+ path = strings.Replace(path, "\\", "/", -1)
+
+ // TODO; See if tar.FileInfoheader() could be used instead
+ // without the pathing issues I encountered
+ h := &tar.Header{
+ Name: path,
+ Size: info.Size(),
+ // I don't like it... but it helps with platform compatibility
+ Mode: 0664,
+ ModTime: info.ModTime(),
+ }
+
+ err = writer.WriteHeader(h)
+ if err != nil {
+ return err
+ }
+
+ _, err = io.Copy(writer, f)
+ if err != nil {
+ // TODO: Figure out how to add more useful information to
+ // These errors
+ // fmt.Fprintln(os.Stderr, f.Name())
+ return err
+ }
+
+ return nil
+ }
+}