Browse Source

调整压缩包存放路径

master
xyiege 3 months ago
parent
commit
73b6ebe0e9
  1. 34
      scagent/util/fsutil.go

34
scagent/util/fsutil.go

@ -14,6 +14,7 @@ import (
"path/filepath" "path/filepath"
"scagent/config" "scagent/config"
"strings" "strings"
"time"
"github.com/schollz/progressbar/v3" "github.com/schollz/progressbar/v3"
"go.uber.org/zap" "go.uber.org/zap"
@ -319,9 +320,7 @@ func DecompressZip(zpFname string, dirpath string) error {
// dest:目的地址以及压缩文件名 eg:/data/logZip/2022-12-12-log.zip // dest:目的地址以及压缩文件名 eg:/data/logZip/2022-12-12-log.zip
// paths:需要压缩的所有文件所组成的集合 // paths:需要压缩的所有文件所组成的集合
func CompressToZip(dest string, currentPath string, paths []string) error { func CompressToZip(dest string, currentPath string, paths []string) error {
// fmt.Printf("currentPath:%s\n", currentPath)
// fmt.Printf("paths len:%d\n", len(paths))
// fmt.Println("real path", currentPath)
// 打开files 目录 // 打开files 目录
filesPath := "" filesPath := ""
if dir, err := os.Getwd(); err == nil { if dir, err := os.Getwd(); err == nil {
@ -339,6 +338,15 @@ func CompressToZip(dest string, currentPath string, paths []string) error {
zipWriter := zip.NewWriter(zfile) zipWriter := zip.NewWriter(zfile)
defer zipWriter.Close() defer zipWriter.Close()
// 目录格式 2025/10
dirpath := fmt.Sprintf("%d/%02d", time.Now().Year(), time.Now().Month())
// 组织目录路径
filesPath = filepath.Join(filesPath, dirpath)
// 创建目录
if err := os.MkdirAll(filesPath, os.ModePerm); err != nil {
return err
}
// 遍历带压缩的目录信息 // 遍历带压缩的目录信息
for _, src := range paths { for _, src := range paths {
// 替换文件名,并且去除前后 "\" 或 "/" // 替换文件名,并且去除前后 "\" 或 "/"
@ -432,3 +440,23 @@ func ReadConfig() {
func DeleteFile(filePath string) error { func DeleteFile(filePath string) error {
return os.Remove(filePath) return os.Remove(filePath)
} }
// 删除过期文件
func DeleteExpiredFiles(dirPath string, expiredTime time.Duration) error {
// 遍历目录下的所有文件
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// 检查文件是否过期
if info.IsDir() {
return nil
}
if time.Since(info.ModTime()) > expiredTime {
// 删除过期文件
return DeleteFile(path)
}
return nil
})
return err
}

Loading…
Cancel
Save