-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (71 loc) · 1.53 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
)
func main() {
rootDir := "."
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && path != rootDir {
if filepath.Dir(path) == rootDir {
return compressDir(path)
}
}
return nil
})
if err != nil {
fmt.Printf("Error walking the path %v: %v\n", rootDir, err)
}
}
// compressDir 压缩指定目录
func compressDir(dirPath string) error {
zipFilePath := dirPath + ".zip"
zipFile, err := os.Create(zipFilePath)
if err != nil {
return err
}
defer zipFile.Close()
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
err = filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// 计算相对路径
relPath, _ := filepath.Rel(filepath.Dir(dirPath), path)
if info.IsDir() {
return nil
}
// 创建文件头
zipHeader, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
zipHeader.Name = filepath.Join(filepath.Base(dirPath), relPath)
zipHeader.Method = zip.Deflate
zipFileWriter, err := zipWriter.CreateHeader(zipHeader)
if err != nil {
return err
}
// 打开源文件
srcFile, err := os.Open(path)
if err != nil {
return err
}
defer srcFile.Close()
// 复制文件内容到 zip
_, err = io.Copy(zipFileWriter, srcFile)
return err
})
if err != nil {
return err
}
fmt.Printf("Compressed %s into %s\n", dirPath, zipFilePath)
return nil
}