-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip.go
121 lines (108 loc) · 2.67 KB
/
zip.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package golib
import (
"archive/zip"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
)
// UnpackZipFile reads zipData and unpacks the contents of the zip
// into targetDir. targetDir must exist.
func UnpackZipFile(targetDir string, zipData io.Reader) (err error) {
zipFile, err := os.CreateTemp(targetDir, "")
if err != nil {
return fmt.Errorf("Unpack ZIP: %w", err)
}
defer func() {
zipFile.Close()
_ = os.Remove(zipFile.Name())
}()
_, err = io.Copy(zipFile, zipData)
if err != nil {
return fmt.Errorf("Copy ZIP: %w", err)
}
r, err := zip.OpenReader(zipFile.Name())
if err != nil {
return fmt.Errorf("Read ZIP: %w", err)
}
// Iterate through the files in the archive,
// printing some of their contents.
for _, f := range r.File {
// Join turns "/" into "\" on Windows
fn := filepath.Join(targetDir, f.Name)
if f.FileInfo().IsDir() {
// Make Folder
err = os.MkdirAll(fn, f.Mode())
if err != nil {
return fmt.Errorf("Mkdir for ZIP %q: %w", f.Name, err)
}
continue
}
// Make directory for file
err = os.MkdirAll(filepath.Dir(fn), 0755)
if err != nil {
return fmt.Errorf("Mkdir for ZIP %q: %w", f.Name, err)
}
// Open file in ZIP
rc, err := f.Open()
if err != nil {
return fmt.Errorf("Open for ZIP %q: %w", f.Name, err)
}
// Open file on disk
of, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return fmt.Errorf("Open on disk for ZIP %q: %w", f.Name, err)
}
// Copy data from ZIP to disk
_, err = io.Copy(of, rc)
if err != nil {
of.Close()
return fmt.Errorf("Copy for ZIP %q: %w", fn, err)
}
of.Close()
rc.Close()
}
r.Close()
return nil
}
// PackZipFile packs all files from sourceDir as ZIP to writeTo. We pack
// all filenames using /. On Windows, the \ in paths is replaced by /
func PackZipFile(sourceDir, topLevelDir string, writeTo io.Writer) (err error) {
sep := string(os.PathSeparator)
zipW := zip.NewWriter(writeTo)
err = filepath.Walk(sourceDir, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
fn := strings.ReplaceAll(strings.TrimPrefix(p, sourceDir+sep), sep, "/")
if topLevelDir != "" {
fn = path.Join(topLevelDir, fn) // Use "/" join here, independent from the OS as it is standard in ZIP
}
fh, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
fh.Name = fn
fw, err := zipW.CreateHeader(fh)
if err != nil {
return err
}
file, err := os.Open(p)
if err != nil {
return err
}
_, err = io.Copy(fw, file)
file.Close()
return err
})
zipW.Close()
if err != nil {
return fmt.Errorf("Error packing ZIP: %w", err)
}
return nil
}