|
|
|
@ -4,6 +4,7 @@ import ( |
|
|
|
"archive/zip" |
|
|
|
"encoding/base64" |
|
|
|
"fmt" |
|
|
|
"fssc/config" |
|
|
|
"io" |
|
|
|
"io/fs" |
|
|
|
"os" |
|
|
|
@ -65,6 +66,7 @@ func CompressToZip(dest string, currentPath string, paths []string) error { |
|
|
|
src = path.Join(currentPath, "/", src) |
|
|
|
// 删除尾随路径(如果它是目录)
|
|
|
|
src := strings.TrimSuffix(src, string(os.PathSeparator)) |
|
|
|
// 文件遍历
|
|
|
|
err = filepath.Walk(src, func(path string, info os.FileInfo, err error) error { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
@ -77,8 +79,11 @@ func CompressToZip(dest string, currentPath string, paths []string) error { |
|
|
|
// 将压缩方法设置为deflate
|
|
|
|
header.Method = zip.Deflate |
|
|
|
|
|
|
|
// 监听的工作目录作为文件的结尾,然后取相对路径
|
|
|
|
header.Name, err = filepath.Rel(filepath.Dir(config.G.FilePath), path) |
|
|
|
|
|
|
|
// 在zip存档中设置文件的相对路径
|
|
|
|
header.Name, err = filepath.Rel(filepath.Dir(src), path) |
|
|
|
//header.Name, err = filepath.Rel(filepath.Dir(src), path)
|
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
@ -91,6 +96,7 @@ func CompressToZip(dest string, currentPath string, paths []string) error { |
|
|
|
// 替换一下分隔符,zip不支持 "\\"
|
|
|
|
header.Name = strings.ReplaceAll(header.Name, "\\", "/") |
|
|
|
} |
|
|
|
// fmt.Printf("zip header:%v\n", header.Name)
|
|
|
|
// 创建写入头的writer
|
|
|
|
headerWriter, err := zipWriter.CreateHeader(header) |
|
|
|
if err != nil { |
|
|
|
@ -107,6 +113,7 @@ func CompressToZip(dest string, currentPath string, paths []string) error { |
|
|
|
_, err = io.Copy(headerWriter, f) |
|
|
|
return err |
|
|
|
}) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|