diff options
| -rw-r--r-- | archive/archive.go | 34 | ||||
| -rw-r--r-- | archive/unarchive.go | 30 | ||||
| -rw-r--r-- | steam-export-cli.go | 13 | ||||
| -rw-r--r-- | steam/package.go | 8 |
4 files changed, 63 insertions, 22 deletions
diff --git a/archive/archive.go b/archive/archive.go index 39ff9f8..6941b64 100644 --- a/archive/archive.go +++ b/archive/archive.go @@ -4,6 +4,8 @@ import ( "archive/tar" "path/filepath" + "compress/gzip" + "io" "os" ) @@ -12,10 +14,9 @@ type Archive struct { Output string Input []string file *os.File - writer *tar.Writer } -func (a *Archive) Tar() error { +func (a *Archive) Tar(compressionType string) error { var err error if a.file, err = os.Create(a.Output); err != nil { return err @@ -23,11 +24,28 @@ func (a *Archive) Tar() error { defer a.file.Close() - a.writer = tar.NewWriter(a.file) - defer a.writer.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, a.tarWalkfn()); err != nil { + if err := filepath.Walk(v, tarWalkfn(twriter)); err != nil { return err } } @@ -36,7 +54,7 @@ func (a *Archive) Tar() error { } -func (a *Archive) tarWalkfn() filepath.WalkFunc { +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 { @@ -62,12 +80,12 @@ func (a *Archive) tarWalkfn() filepath.WalkFunc { ModTime: info.ModTime(), } - err = a.writer.WriteHeader(h) + err = writer.WriteHeader(h) if err != nil { return err } - _, err = io.Copy(a.writer, f) + _, err = io.Copy(writer, f) if err != nil { // TODO: Figure out how to add more useful information to // These errors diff --git a/archive/unarchive.go b/archive/unarchive.go index 5089370..d283d4e 100644 --- a/archive/unarchive.go +++ b/archive/unarchive.go @@ -4,29 +4,43 @@ import ( "archive/tar" fp "path/filepath" + "compress/gzip" + "io" "os" ) type Unarchive struct { Input string - inputFile *os.File tarReader *tar.Reader } // Extracts a tar arcive to the current working directory // This will overwrite everything. So be careful -func (u *Unarchive) UnTar() error { - var err error - u.inputFile, err = os.Open(u.Input) +func (u *Unarchive) UnTar(compressionType string) error { + f, err := os.Open(u.Input) if err != nil { return err } - defer u.inputFile.Close() - u.tarReader = tar.NewReader(u.inputFile) + defer f.Close() + + var treader *tar.Reader + + switch compressionType { + case "gz": + gzreader, err := gzip.NewReader(f) + if err != nil { + return err + } + // Read from the gzip reader instead of the file + treader = tar.NewReader(gzreader) + default: + // Read from the file directly + treader = tar.NewReader(f) + } for { - hdr, err := u.tarReader.Next() + hdr, err := treader.Next() if err == io.EOF { // We've reached the end! Whoee break @@ -54,7 +68,7 @@ func (u *Unarchive) UnTar() error { return err } defer f.Close() - if _, err := io.Copy(f, u.tarReader); err != nil { + if _, err := io.Copy(f, treader); err != nil { return err } diff --git a/steam-export-cli.go b/steam-export-cli.go index 3b9d607..23857e0 100644 --- a/steam-export-cli.go +++ b/steam-export-cli.go @@ -91,6 +91,8 @@ func packageGame(args []string) error { "Name of the archive file to be created. Please do not include the file extension") game := fl.Int("g", -1, "Index of the game to be exported. Please see `list` for the index") + compress := fl.String("z", "gz", + "Compression type. Default 'gz' '' is no compression") var g int l := steam.DefaultLib @@ -112,7 +114,12 @@ func packageGame(args []string) error { fileName = &steamLib.Games[g] } - return steamLib.PackageGameToFile(g, *fileName+".tar") + switch *compress { + case "gz": + return steamLib.PackageGameToFile(g, *fileName+".tar.gz", *compress) + default: + return steamLib.PackageGameToFile(g, *fileName+".tar", *compress) + } } @@ -122,6 +129,8 @@ func extractGame(args []string) error { "Path to library in question. All the way to the 'steamapps' folder") fileName := fl.String("f", "", "Name of the archive file to be extracted. Please include the file extension") + compress := fl.String("z", "gz", + "Compression type. Default 'gz' '' is no compression") l := steam.DefaultLib fl.Parse(args) @@ -137,7 +146,7 @@ func extractGame(args []string) error { return errors.New("No filename provided") } - return steamLib.ExtractGameFromFile(*fileName) + return steamLib.ExtractGameFromFile(*fileName, *compress) } diff --git a/steam/package.go b/steam/package.go index d7e33af..efa2f24 100644 --- a/steam/package.go +++ b/steam/package.go @@ -7,7 +7,7 @@ import ( "path/filepath" ) -func (l *Library) PackageGameToFile(index int, file string) error { +func (l *Library) PackageGameToFile(index int, file, compress string) error { g := l.Games[index] working_dir, err := os.Getwd() @@ -27,7 +27,7 @@ func (l *Library) PackageGameToFile(index int, file string) error { } input := []string{"common/" + g, acf} a := archive.Archive{Output: output, Input: input} - err = a.Tar() + err = a.Tar(compress) if err != nil { return err } @@ -37,7 +37,7 @@ func (l *Library) PackageGameToFile(index int, file string) error { return nil } -func (l *Library) ExtractGameFromFile(f string) error { +func (l *Library) ExtractGameFromFile(f, compress string) error { working_dir, err := os.Getwd() if err != nil { return err @@ -54,7 +54,7 @@ func (l *Library) ExtractGameFromFile(f string) error { u := &archive.Unarchive{ Input: f, } - if err := u.UnTar(); err != nil { + if err := u.UnTar(compress); err != nil { return err } |
