fix: 解决 zip 压缩失败之后压缩文件还存在的问题 (#3459)

Refs https://github.com/1Panel-dev/1Panel/issues/3012
This commit is contained in:
zhengkunwang 2023-12-27 11:40:09 +08:00 committed by GitHub
parent ee2fa70bb0
commit aaafeb039e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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
}