X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=recursive_decompress.go;h=4c2dceeca9abc26362a5f0e763333b1e3ccb17c8;hb=b3ae7d8c427f9dc45db9eb7117cb2b609e4bfae5;hp=a642fa7232ee11a802b7f1663297455e64d35b35;hpb=cb7cd6e03095ac7acbfeb2c74a587c4f053ef6bc;p=cmccabe-bin diff --git a/recursive_decompress.go b/recursive_decompress.go index a642fa7..4c2dcee 100644 --- a/recursive_decompress.go +++ b/recursive_decompress.go @@ -28,13 +28,12 @@ func processPath(curPath string) error { } } } else { - tarBaseName := tarBaseName(curPath) - if tarBaseName != "" { - err = applyTarOn(curPath) - if err != nil { - return err - } - err = processPath(filepath.Join(filepath.Dir(curPath), tarBaseName)) + extractedBaseName, err := extractIfPossible(curPath) + if err != nil { + return err + } + if extractedBaseName != "" { + err = processPath(filepath.Join(filepath.Dir(curPath), extractedBaseName)) if err != nil { return err } @@ -50,23 +49,79 @@ func processPath(curPath string) error { return nil } -func tarBaseName(curPath string) string { +func extractIfPossible(curPath string) (string, error) { if strings.HasSuffix(curPath, ".tgz") { - return filepath.Base(curPath[:len(curPath)-4]) + 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 "" + return "", nil } -func applyTarOn(curPath 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) //cmd.Stdout = os.Stdout err := cmd.Run() if err != nil { - return fmt.Errorf("error running tar xvf on %s: %s", curPath, err.Error()) + return "", fmt.Errorf("error running tar xvf on %s: %s", curPath, err.Error()) } - return nil + return filepath.Base(curPath[:len(curPath) - len(suffix)]), nil +} + +func extract7z(curPath string) (string, error) { + fmt.Printf("== decompressing 7z file %s\n", curPath) + extractedBaseName := curPath[:len(curPath) - len(".7z")] + 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("7z", "x", absCurPath) + cmd.Dir = absExtractedBaseName + //cmd.Stdout = os.Stdout + err = cmd.Run() + if err != nil { + return "", fmt.Errorf("error running 7z on %s: %s", curPath, err.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() {