Browse Source

调整解压文件存在情况

master
xyiege 1 month ago
parent
commit
e626fdaf32
  1. 22
      scagent/util/fsutil.go

22
scagent/util/fsutil.go

@ -288,17 +288,6 @@ func DecompressZip(zpFname string, dirpath string) error {
os.MkdirAll(filePath, os.ModePerm)
continue
}
// 文件存在先备份再覆盖
if _, err := os.Stat(filePath); err == nil {
// 当前时间
currentTime := time.Now().Format("20060102_150405")
// 备份文件
backupPath := filePath + currentTime + "_bak"
if err := os.Rename(filePath, backupPath); err != nil {
return fmt.Errorf("failed to backup file (%v)", err)
}
continue
}
// 父文件夹开始创建目录
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
return fmt.Errorf("failed to make directory (%v)", err)
@ -312,6 +301,17 @@ func DecompressZip(zpFname string, dirpath string) error {
if err != nil {
return fmt.Errorf("failed to open file in zip (%v)", err)
}
// 检查文件是否存在,存在的话先备份
if _, err := os.Stat(filePath); err == nil {
// 当前时间
currentTime := time.Now().Format("20060102_150405")
// 备份文件
backupPath := filePath + currentTime + "_bak"
if err := os.Rename(filePath, backupPath); err != nil {
return fmt.Errorf("failed to backup file (%v)", err)
}
}
// 复制文件到目标路径
if _, err := io.Copy(dstFile, fileInArchive); err != nil {
return fmt.Errorf("failed to copy file in zip (%v)", err)
}

Loading…
Cancel
Save