forked from alexellis/actions-batch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unzip.go
98 lines (86 loc) · 1.91 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
package main
import (
"archive/zip"
"fmt"
"io"
"log"
"os"
"path"
"path/filepath"
"time"
)
// Unzip reads the compressed zip file from reader and writes it into dir.
// Unzip works similar to Untar where support for nested folders is removed
// so that all files are placed in the same target directory
func Unzip(reader io.ReaderAt, size int64, dir string, quiet bool) error {
zipReader, err := zip.NewReader(reader, size)
if err != nil {
return fmt.Errorf("error creating zip reader: %s", err)
}
return unzip(*zipReader, dir, quiet)
}
func unzip(r zip.Reader, dir string, quiet bool) (err error) {
if err != nil {
return err
}
t0 := time.Now()
nFiles := 0
madeDir := map[string]bool{}
defer func() {
td := time.Since(t0)
if err == nil {
if !quiet {
log.Printf("extracted zip into %s: %d files, %d dirs (%v)", dir, nFiles, len(madeDir), td)
}
} else {
log.Printf("error extracting zip into %s after %d files, %d dirs, %v: %v", dir, nFiles, len(madeDir), td, err)
}
}()
// Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()
baseFile := filepath.Base(f.Name)
abs := path.Join(dir, baseFile)
if !quiet {
fmt.Printf("Extracting: %s\n", abs)
}
fi := f.FileInfo()
mode := fi.Mode()
switch {
case mode.IsDir():
break
case mode.IsRegular():
f, err := os.OpenFile(abs, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
default:
}
nFiles++
return nil
}
for _, f := range r.File {
err := extractAndWriteFile(f)
if err != nil {
return err
}
}
return nil
}