aboutsummaryrefslogtreecommitdiff
path: root/lib/archive/unarchive.go
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2017-02-04 18:16:22 -0500
committerMitch Riedstra <mitch@riedstra.us>2017-02-04 18:16:22 -0500
commit0e346bf5ad4852db5db82343b2aca5911c38ad00 (patch)
tree5a2528211318da0c550dfb5ca576a0292ed8176b /lib/archive/unarchive.go
parent41752a0cf50735bc29dae18271539719f7b59f7c (diff)
downloadsteam-export-0e346bf5ad4852db5db82343b2aca5911c38ad00.tar.gz
steam-export-0e346bf5ad4852db5db82343b2aca5911c38ad00.tar.xz
Moved the libraries around
Diffstat (limited to 'lib/archive/unarchive.go')
-rw-r--r--lib/archive/unarchive.go85
1 files changed, 85 insertions, 0 deletions
diff --git a/lib/archive/unarchive.go b/lib/archive/unarchive.go
new file mode 100644
index 0000000..8a5617e
--- /dev/null
+++ b/lib/archive/unarchive.go
@@ -0,0 +1,85 @@
+package archive
+
+import (
+ "archive/tar"
+ fp "path/filepath"
+
+ "compress/gzip"
+
+ "io"
+ "os"
+
+ "strings"
+)
+
+type Unarchive struct {
+ Input string
+ tarReader *tar.Reader
+}
+
+// Extracts a tar arcive to the current working directory
+// This will overwrite everything. So be careful
+func (u *Unarchive) UnTar(compressionType string) error {
+ f, err := os.Open(u.Input)
+ if err != nil {
+ return err
+ }
+ 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 := treader.Next()
+ if err == io.EOF {
+ // We've reached the end! Whoee
+ break
+ }
+ if err != nil {
+ return err
+ }
+
+ // Fix windows slashes...
+ fileName := strings.Replace(hdr.Name, "\\", "/", -1)
+
+ info := hdr.FileInfo()
+ if info.IsDir() {
+ // I don't like hard-coded permissions but it
+ // it helps with overall platform compatibility
+ if err = os.MkdirAll(fileName, 0775); err != nil {
+ return err
+ }
+ continue
+ }
+
+ if err = os.MkdirAll(fp.Dir(fileName), 0775); err != nil {
+ return err
+ }
+
+ // Create a file handle to work with
+ // f, err := os.Create(fileName)
+ f, err := os.OpenFile(fileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0664)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+ if _, err := io.Copy(f, treader); err != nil {
+ return err
+ }
+
+ }
+
+ return nil
+}