-
Notifications
You must be signed in to change notification settings - Fork 0
/
unzip.go
116 lines (107 loc) · 2.27 KB
/
unzip.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
// Package unzip implements unzipping for jarcat.
// We implement this to avoid needing a runtime dependency on unzip,
// which is not a profound package but not installed everywhere by default.
package unzip
import (
"io"
"os"
"path"
"strings"
"sync"
"third_party/go/zip"
)
// concurrency controls the maximum level of concurrency we'll allow.
const concurrency = 4
// Extract extracts the contents of the given zipfile.
func Extract(in, out, file, prefix string) error {
e := extractor{
In: in,
Out: out,
File: file,
Prefix: prefix,
dirs: map[string]struct{}{},
}
return e.Extract()
}
// An extractor extracts a single zipfile.
type extractor struct {
In string
Out string
File string
Prefix string
dirs map[string]struct{}
mutex sync.Mutex
wg sync.WaitGroup
err error
}
func (e *extractor) Extract() error {
r, err := zip.OpenReader(e.In)
if err != nil {
return err
}
defer r.Close()
ch := make(chan *zip.File, 100)
for i := 0; i < concurrency; i++ {
go e.consume(ch)
}
for _, f := range r.File {
if e.File != "" && f.Name != e.File {
continue
}
// This will mean that empty directories aren't created. We might need to fix that at some point.
if f.Mode()&os.ModeDir == 0 {
e.wg.Add(1)
ch <- f
}
}
e.wg.Wait()
close(ch)
return e.err
}
func (e *extractor) consume(ch <-chan *zip.File) {
for f := range ch {
if err := e.extractFile(f); err != nil {
e.err = err
}
e.wg.Done()
}
}
func (e *extractor) extractFile(f *zip.File) error {
if e.Prefix != "" {
if !strings.HasPrefix(f.Name, e.Prefix) {
return nil
}
f.Name = strings.TrimLeft(strings.TrimPrefix(f.Name, e.Prefix), "/")
}
r, err := f.Open()
if err != nil {
return err
}
defer r.Close()
out := path.Join(e.Out, f.Name)
if e.File != "" {
out = e.Out
}
if err := e.makeDir(out); err != nil {
return err
}
o, err := os.OpenFile(out, os.O_WRONLY|os.O_CREATE, f.Mode())
if err != nil {
return err
}
defer o.Close()
_, err = io.Copy(o, r)
return err
}
func (e *extractor) makeDir(filename string) error {
dir := path.Dir(filename)
e.mutex.Lock()
defer e.mutex.Unlock()
if _, present := e.dirs[dir]; !present {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
e.dirs[dir] = struct{}{}
}
return nil
}