|
@@ -1,7 +1,6 @@
|
|
|
package util
|
|
|
|
|
|
import (
|
|
|
- "bytes"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
"io/fs"
|
|
@@ -12,38 +11,27 @@ import (
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
-// 压缩指定目录到ZIP文件,并排除一个指定的子目录
|
|
|
-func ZipDir(src, dest string, excludeDirs []string) error {
|
|
|
- // 构造排除参数的字符串
|
|
|
- excludeArgs := make([]string, 0, len(excludeDirs))
|
|
|
- for _, excludeDir := range excludeDirs {
|
|
|
- relExcludeDir, err := filepath.Rel(src, excludeDir)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- // 确保排除路径以 '*' 结尾,以匹配目录下的所有内容
|
|
|
- excludeArgs = append(excludeArgs, filepath.Join(relExcludeDir, "*"))
|
|
|
+// 将指定的目录和子目录压缩成 ZIP 文件
|
|
|
+func ZipDir(src string, dest string, dirs []string) error {
|
|
|
+ // 创建 zip 命令参数
|
|
|
+ args := []string{"-r", dest}
|
|
|
+ for _, dir := range dirs {
|
|
|
+ args = append(args, filepath.Join(src, dir))
|
|
|
}
|
|
|
|
|
|
- // 拼接 -x 参数
|
|
|
- excludeStr := strings.Join(excludeArgs, " -x ")
|
|
|
+ // 创建 zip 命令
|
|
|
+ cmd := exec.Command("zip", args...)
|
|
|
|
|
|
- // 构建zip命令
|
|
|
- cmd := exec.Command("zip", append([]string{"-r", dest, "."}, strings.Split(excludeStr, " ")...)...)
|
|
|
-
|
|
|
- // 设置工作目录为要压缩的源目录
|
|
|
+ // 设置工作目录
|
|
|
cmd.Dir = src
|
|
|
|
|
|
- // 捕获命令的标准输出和标准错误输出
|
|
|
- var out bytes.Buffer
|
|
|
- cmd.Stdout = &out
|
|
|
- cmd.Stderr = &out
|
|
|
-
|
|
|
- // 运行命令
|
|
|
- if err := cmd.Run(); err != nil {
|
|
|
- return fmt.Errorf("zip command failed: %v, output: %s", err, out.String())
|
|
|
+ // 执行 zip 命令并获取输出
|
|
|
+ output, err := cmd.CombinedOutput()
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("error while executing zip command: %v\nOutput: %s", err, output)
|
|
|
}
|
|
|
|
|
|
+ fmt.Printf("ZIP file created successfully: %s\n", dest)
|
|
|
return nil
|
|
|
}
|
|
|
|