-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackit.go
More file actions
200 lines (167 loc) · 4.63 KB
/
packit.go
File metadata and controls
200 lines (167 loc) · 4.63 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package packit
import (
"archive/tar"
"fmt"
"io"
"os"
"strings"
"sync"
"github.com/c4milo/fastwalk"
"github.com/dsnet/compress/bzip2"
"github.com/klauspost/compress/zip"
gzip "github.com/klauspost/pgzip"
"github.com/pkg/errors"
"github.com/ulikunitz/xz"
)
// Zip walks the file tree rooted at root and archives it into the output stream using zip
// algorithm.
func Zip(root string, out io.Writer) {
zw := zip.NewWriter(out)
defer func() {
if err := zw.Close(); err != nil {
fmt.Printf("packit/zip: failed closing archive: %v\n", err)
}
}()
sep := string(os.PathSeparator)
m := &sync.Mutex{}
fastwalk.Walk(root, func(path string, mode os.FileMode) error {
// This function gets invoked concurrently for each file or directory found.
// But, the zip package does not support parallelism, so we need to make sure
// a file header is followed by its correspondent content.
m.Lock()
defer m.Unlock()
// Appending a final "/" is critical to let the decompressor know this is a directory.
if mode.IsDir() && !strings.HasSuffix(path, sep) {
path += sep
}
fi, err := os.Lstat(path)
if err != nil {
return errors.Wrapf(err, "packit/zip: failed getting file stats for: %s", path)
}
fh, err := zip.FileInfoHeader(fi)
if err != nil {
return errors.Wrapf(err, "packit/zip: failed populating file header for: %s", path)
}
// fi.Name returns the base name and we need the full path.
fh.Name = path
fw, err := zw.CreateHeader(fh)
if err != nil {
return errors.Wrapf(err, "packit/zip: failed creating header for: %s", path)
}
// For directories, we only need to add the header in the zip file.
if mode.IsDir() {
return nil
}
f, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "packit/zip: failed opening file at: %s", path)
}
defer func() {
if err := f.Close(); err != nil {
fmt.Printf("packit/zip: failed closing file %q: %v\n", path, err)
}
}()
_, err = io.Copy(fw, f)
if err != nil {
return errors.Wrapf(err, "packit/zip: failed packing data from: %s", path)
}
return nil
})
}
// Tar walks the file tree rooted at root and archives it all using Tar.
func Tar(root string, out io.Writer) {
tw := tar.NewWriter(out)
defer func() {
if err := tw.Close(); err != nil {
fmt.Printf("packit/tar: failed closing archive: %v\n", err)
}
}()
sep := string(os.PathSeparator)
m := &sync.Mutex{}
fastwalk.Walk(root, func(path string, mode os.FileMode) error {
m.Lock()
defer m.Unlock()
// Appending a trailing path separator is critical to let the decompressor
// know this is a directory.
if mode.IsDir() && !strings.HasSuffix(path, sep) {
path += sep
}
fi, err := os.Lstat(path)
if err != nil {
return errors.Wrapf(err, "packit/tar: failed getting file stats for: %s", path)
}
fh, err := tar.FileInfoHeader(fi, "")
if err != nil {
return errors.Wrapf(err, "packit/tar: failed populating file header for: %s", path)
}
// fi.Name returns the base name and we need the full path.
fh.Name = path
tw.WriteHeader(fh)
if err != nil {
return errors.Wrapf(err, "packit/tar: failed creating header for: %s", path)
}
// For directories, we only need to add the header in the tar file.
if mode.IsDir() {
return nil
}
f, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "packit/tar: failed opening file at: %s", path)
}
defer func() {
if err := f.Close(); err != nil {
fmt.Printf("packit/tar: failed closing file: %s\n", path)
}
}()
_, err = io.Copy(tw, f)
if err != nil {
return errors.Wrapf(err, "packit/tar: failed packing data from: %s", path)
}
return nil
})
}
// Gzip compresses an input stream using gzip.
func Gzip(in io.Reader, out io.Writer) error {
gw := gzip.NewWriter(out)
defer func() {
if err := gw.Close(); err != nil {
fmt.Printf("packit/gzip: failed closing stream: %v\n", err)
}
}()
if _, err := io.Copy(gw, in); err != nil {
return err
}
return nil
}
// Xz compresses an input stream using xz.
func Xz(in io.Reader, out io.Writer) error {
xw, err := xz.NewWriter(out)
if err != nil {
return err
}
defer func() {
if err := xw.Close(); err != nil {
fmt.Printf("packit/xz: failed closing stream: %v\n", err)
}
}()
if _, err := io.Copy(xw, in); err != nil {
return err
}
return nil
}
// Bzip2 compresses an input stream using bzip2.
func Bzip2(in io.Reader, out io.Writer) error {
bw, err := bzip2.NewWriter(out, nil)
if err != nil {
return err
}
defer func() {
if err := bw.Close(); err != nil {
fmt.Printf("packit/bzip2: failed closing stream: %v\n", err)
}
}()
if _, err := io.Copy(bw, in); err != nil {
return err
}
return nil
}