-
Notifications
You must be signed in to change notification settings - Fork 5
/
gzip.go
117 lines (110 loc) · 2.37 KB
/
gzip.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
package utils
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
)
// ExtractGzip extracts a gzipped archive to the specified directory and
// returns the path to the root level of the extracted archive.
// Caller must ensure that the destination directory exists.
func ExtractGzip(file, destDir string, skipHidden bool) (root string, err error) {
var (
f *os.File
gr *gzip.Reader
)
// Read the gzip file.
if f, err = os.Open(file); err != nil {
return "", err
}
defer f.Close()
if gr, err = gzip.NewReader(f); err != nil {
return "", err
}
defer gr.Close()
// Write the contents to the temporary directory.
tr := tar.NewReader(gr)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return "", err
}
switch hdr.Typeflag {
case tar.TypeDir:
if err = os.MkdirAll(filepath.Join(destDir, hdr.Name), os.FileMode(hdr.Mode)); err != nil {
return "", err
}
if root == "" {
root = filepath.Join(destDir, hdr.Name)
}
case tar.TypeReg:
var reg *os.File
if skipHidden && hdr.Name[0] == '.' {
// Skip hidden files if requested.
continue
}
if reg, err = os.Create(filepath.Join(destDir, hdr.Name)); err != nil {
return "", err
}
if _, err = io.Copy(reg, tr); err != nil {
reg.Close()
return "", err
}
reg.Close()
default:
return "", fmt.Errorf("extracting %s: unknown type flag: %c", hdr.Name, hdr.Typeflag)
}
}
return root, nil
}
// WriteGzip compresses a directory to a gzipped archive.
func WriteGzip(dir, file string) (err error) {
var (
f *os.File
)
// Create a gzip file.
if f, err = os.Create(file); err != nil {
return err
}
defer f.Close()
gw := gzip.NewWriter(f)
defer gw.Close()
// Create a tar file.
tw := tar.NewWriter(gw)
defer tw.Close()
// Write the DB to the tar file.
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
var hdr *tar.Header
if hdr, err = tar.FileInfoHeader(info, ""); err != nil {
return err
}
hdr.Name = path[len(dir):]
if err = tw.WriteHeader(hdr); err != nil {
return err
}
if info.IsDir() {
return nil
}
var tmp *os.File
if tmp, err = os.Open(path); err != nil {
return err
}
defer tmp.Close()
if _, err = io.Copy(tw, tmp); err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}