diff --git a/backend/utils/files/file_op.go b/backend/utils/files/file_op.go index 913a65d37..e54ca6f3d 100644 --- a/backend/utils/files/file_op.go +++ b/backend/utils/files/file_op.go @@ -613,6 +613,9 @@ func (f FileOp) decompressWithSDK(srcFile string, dst string, cType CompressType func (f FileOp) Decompress(srcFile string, dst string, cType CompressType, secret string) error { if cType == Tar || cType == Zip || cType == TarGz { shellArchiver, err := NewShellArchiver(cType) + if !f.Stat(dst) { + _ = f.CreateDir(dst, 0755) + } if err == nil { if err = shellArchiver.Extract(srcFile, dst, secret); err == nil { return nil diff --git a/backend/utils/files/tar.go b/backend/utils/files/tar.go index 6aaa1f035..4ab276c97 100644 --- a/backend/utils/files/tar.go +++ b/backend/utils/files/tar.go @@ -18,7 +18,7 @@ func NewTarArchiver(compressType CompressType) ShellArchiver { } func (t TarArchiver) Extract(FilePath string, dstDir string, secret string) error { - return cmd.ExecCmd(fmt.Sprintf("%s %s %s -C %s", t.Cmd, t.getOptionStr("extract"), FilePath, dstDir)) + return cmd.ExecCmd(fmt.Sprintf("%s %s \"%s\" -C \"%s\"", t.Cmd, t.getOptionStr("extract"), FilePath, dstDir)) } func (t TarArchiver) Compress(sourcePaths []string, dstFile string, secret string) error { diff --git a/backend/utils/files/tar_gz.go b/backend/utils/files/tar_gz.go index 8a1691abf..1839639ef 100644 --- a/backend/utils/files/tar_gz.go +++ b/backend/utils/files/tar_gz.go @@ -20,11 +20,11 @@ func (t TarGzArchiver) Extract(filePath, dstDir string, secret string) error { var err error commands := "" if len(secret) != 0 { - extraCmd := "openssl enc -d -aes-256-cbc -k '" + secret + "' -in " + filePath + " | " - commands = fmt.Sprintf("%s tar -zxvf - -C %s", extraCmd, dstDir+" > /dev/null 2>&1") + extraCmd := fmt.Sprintf("openssl enc -d -aes-256-cbc -k '%s' -in '%s' | ", secret, filePath) + commands = fmt.Sprintf("%s tar -zxvf - -C '%s' > /dev/null 2>&1", extraCmd, dstDir) global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******")) } else { - commands = fmt.Sprintf("tar -zxvf %s %s", filePath+" -C ", dstDir+" > /dev/null 2>&1") + commands = fmt.Sprintf("tar -zxvf '%s' -C '%s' > /dev/null 2>&1", filePath, dstDir) global.LOG.Debug(commands) } if err = cmd.ExecCmd(commands); err != nil { @@ -34,27 +34,25 @@ func (t TarGzArchiver) Extract(filePath, dstDir string, secret string) error { } func (t TarGzArchiver) Compress(sourcePaths []string, dstFile string, secret string) error { - var err error - path := "" - itemDir := "" + var itemDirs []string for _, item := range sourcePaths { - itemDir += filepath.Base(item) + " " + itemDirs = append(itemDirs, fmt.Sprintf("\"%s\"", filepath.Base(item))) } - aheadDir := dstFile[:strings.LastIndex(dstFile, "/")] + itemDir := strings.Join(itemDirs, " ") + aheadDir := filepath.Dir(sourcePaths[0]) if len(aheadDir) == 0 { aheadDir = "/" } - path += fmt.Sprintf("- -C %s %s", aheadDir, itemDir) commands := "" if len(secret) != 0 { - extraCmd := "| openssl enc -aes-256-cbc -salt -k '" + secret + "' -out" - commands = fmt.Sprintf("tar -zcf %s %s %s", path, extraCmd, dstFile) + extraCmd := fmt.Sprintf("| openssl enc -aes-256-cbc -salt -k '%s' -out '%s'", secret, dstFile) + commands = fmt.Sprintf("tar -zcf - -C \"%s\" %s %s", aheadDir, itemDir, extraCmd) global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******")) } else { - commands = fmt.Sprintf("tar -zcf %s -C %s %s", dstFile, aheadDir, itemDir) + commands = fmt.Sprintf("tar -zcf \"%s\" -C \"%s\" %s", dstFile, aheadDir, itemDir) global.LOG.Debug(commands) } - if err = cmd.ExecCmd(commands); err != nil { + if err := cmd.ExecCmd(commands); err != nil { return err } return nil