X-Git-Url: http://www.club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?p=cmccabe-bin;a=blobdiff_plain;f=recursive_decompress.go;fp=recursive_decompress.go;h=4c2dceeca9abc26362a5f0e763333b1e3ccb17c8;hp=89d3d746e1954311437a86465c74dd671a52f8fd;hb=37227cd97b103c102365830ef122f08013daf60a;hpb=f1eae14fa3fc60b16263f1f308716ac12cf587e8 diff --git a/recursive_decompress.go b/recursive_decompress.go index 89d3d74..4c2dcee 100644 --- a/recursive_decompress.go +++ b/recursive_decompress.go @@ -51,14 +51,18 @@ func processPath(curPath string) error { func extractIfPossible(curPath string) (string, error) { if strings.HasSuffix(curPath, ".tgz") { - return extractTgz(curPath) + return extractTgz(curPath, ".tgz") + } else if strings.HasSuffix(curPath, ".tar.gz") { + return extractTgz(curPath, ".tar.gz") } else if strings.HasSuffix(curPath, ".7z") { return extract7z(curPath) + } else if strings.HasSuffix(curPath, ".zip") { + return extractZip(curPath) } return "", nil } -func extractTgz(curPath string) (string, error) { +func extractTgz(curPath string, suffix string) (string, error) { fmt.Printf("== decompressing tar file %s\n", curPath) cmd := exec.Command("tar", "xvf", filepath.Base(curPath)) cmd.Dir = filepath.Dir(curPath) @@ -67,7 +71,7 @@ func extractTgz(curPath string) (string, error) { if err != nil { return "", fmt.Errorf("error running tar xvf on %s: %s", curPath, err.Error()) } - return filepath.Base(curPath[:len(curPath) - len(".tgz")]), nil + return filepath.Base(curPath[:len(curPath) - len(suffix)]), nil } func extract7z(curPath string) (string, error) { @@ -87,7 +91,7 @@ func extract7z(curPath string) (string, error) { } cmd := exec.Command("7z", "x", absCurPath) cmd.Dir = absExtractedBaseName - cmd.Stdout = os.Stdout + //cmd.Stdout = os.Stdout err = cmd.Run() if err != nil { return "", fmt.Errorf("error running 7z on %s: %s", curPath, err.Error()) @@ -95,6 +99,31 @@ func extract7z(curPath string) (string, error) { return extractedBaseName, nil } +func extractZip(curPath string) (string, error) { + fmt.Printf("== decompressing zip file %s\n", curPath) + extractedBaseName := curPath[:len(curPath) - len(".zip")] + err := os.Mkdir(extractedBaseName, 0755) + if err != nil { + return "", fmt.Errorf("unable to mkdir '%s': %s", extractedBaseName, err.Error()) + } + absCurPath, err := filepath.Abs(curPath) + if err != nil { + return "", fmt.Errorf("unable to get absolute path for '%s': %s", curPath, err.Error()) + } + absExtractedBaseName, err := filepath.Abs(extractedBaseName) + if err != nil { + return "", fmt.Errorf("unable to get absolute path for '%s': %s", extractedBaseName, err.Error()) + } + cmd := exec.Command("unzip", absCurPath) + cmd.Dir = absExtractedBaseName + //cmd.Stdout = os.Stdout + err = cmd.Run() + if err != nil { + return "", fmt.Errorf("error running unzip on %s: %s", curPath, err.Error()) + } + return extractedBaseName, nil +} + func main() { removeArchives = flag.Bool("r", false, "Remove archive files after decompressing them") flag.Usage = func() {