From aaafeb039eb3c51d85a58ff022a2787a322786d7 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Wed, 27 Dec 2023 11:40:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=20zip=20=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9=E5=A4=B1=E8=B4=A5=E4=B9=8B=E5=90=8E=E5=8E=8B=E7=BC=A9?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=BF=98=E5=AD=98=E5=9C=A8=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#3459)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs https://github.com/1Panel-dev/1Panel/issues/3012 --- backend/utils/files/zip.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/backend/utils/files/zip.go b/backend/utils/files/zip.go index a29c36f88..39f6f4ec6 100644 --- a/backend/utils/files/zip.go +++ b/backend/utils/files/zip.go @@ -25,10 +25,14 @@ func (z ZipArchiver) Extract(filePath, dstDir string) error { } func (z ZipArchiver) Compress(sourcePaths []string, dstFile string) error { + var err error tmpFile := path.Join(global.CONF.System.TmpDir, fmt.Sprintf("%s%s.zip", common.RandStr(50), time.Now().Format("20060102150405"))) op := NewFileOp() defer func() { _ = op.DeleteFile(tmpFile) + if err != nil { + _ = op.DeleteFile(dstFile) + } }() baseDir := path.Dir(sourcePaths[0]) relativePaths := make([]string, len(sourcePaths)) @@ -36,8 +40,11 @@ func (z ZipArchiver) Compress(sourcePaths []string, dstFile string) error { relativePaths[i] = path.Base(sp) } cmdStr := fmt.Sprintf("zip -qr %s %s", tmpFile, strings.Join(relativePaths, " ")) - if err := cmd.ExecCmdWithDir(cmdStr, baseDir); err != nil { + if err = cmd.ExecCmdWithDir(cmdStr, baseDir); err != nil { return err } - return op.Mv(tmpFile, dstFile) + if err = op.Mv(tmpFile, dstFile); err != nil { + return err + } + return nil }