github.com/mondo192/jfrog-client-go@v1.0.0/utils/archiveutils.go (about) 1 package utils 2 3 import ( 4 "github.com/mondo192/jfrog-client-go/utils/errorutils" 5 "github.com/mondo192/jfrog-client-go/utils/io/fileutils" 6 "github.com/mondo192/jfrog-client-go/utils/log" 7 "os" 8 "path/filepath" 9 "strings" 10 ) 11 12 // localPath - The path of the downloaded archive file. 13 // localFileName - name of the archive file. 14 // originFileName - name of the archive file in Artifactory. 15 // logMsgPrefix - prefix log message. 16 // Extract an archive file to the 'localPath'. 17 func ExtractArchive(localPath, localFileName, originFileName, logMsgPrefix string) error { 18 if !fileutils.IsSupportedArchive(originFileName) { 19 return nil 20 } 21 extractionPath, err := getExtractionPath(localPath) 22 if err != nil { 23 return err 24 } 25 var archivePath string 26 if !strings.HasPrefix(localFileName, localPath) { 27 archivePath = filepath.Join(localPath, localFileName) 28 } else { 29 archivePath = localFileName 30 } 31 archivePath, err = filepath.Abs(archivePath) 32 if err != nil { 33 return err 34 } 35 err = os.MkdirAll(extractionPath, 0755) 36 if errorutils.CheckError(err) != nil { 37 return err 38 } 39 log.Info(logMsgPrefix+"Extracting archive:", archivePath, "to", extractionPath) 40 return extract(archivePath, originFileName, extractionPath) 41 } 42 43 func extract(localFilePath, originArchiveName, extractionPath string) error { 44 err := fileutils.Unarchive(localFilePath, originArchiveName, extractionPath) 45 if err != nil { 46 return err 47 } 48 // If the file was extracted successfully, remove it from the file system 49 return errorutils.CheckError(os.Remove(localFilePath)) 50 } 51 52 func getExtractionPath(localPath string) (string, error) { 53 // The local path to which the file is going to be extracted, 54 // needs to be absolute. 55 absolutePath, err := filepath.Abs(localPath) 56 if err != nil { 57 return "", errorutils.CheckError(err) 58 } 59 // Add a trailing slash to the local path, since it has to be a directory. 60 return absolutePath + string(os.PathSeparator), nil 61 }