-
Notifications
You must be signed in to change notification settings - Fork 5
/
unarchive.go
56 lines (43 loc) · 1.05 KB
/
unarchive.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
//+build !windows
package update
import (
"archive/tar"
"compress/gzip"
"io"
"reflect"
"bytes"
"strings"
"github.com/containerum/chkit/pkg/chkitErrors"
)
// unpack .tar.gz archive to temporary dir and save paths
func unarchive(rd io.Reader, update *Package) error {
fmap := update.getFileMap()
retVal := reflect.ValueOf(update).Elem()
gzf, err := gzip.NewReader(io.LimitReader(rd, MaxFileSize))
if err != nil {
return chkitErrors.Wrap(ErrUnpack, err)
}
defer gzf.Close()
tarf := tar.NewReader(gzf)
for {
header, nextErr := tarf.Next()
if nextErr == io.EOF {
break
}
if nextErr != nil {
return chkitErrors.Wrap(ErrUnpack, nextErr)
}
if header.Typeflag == tar.TypeReg {
field, updateFile := fmap[strings.TrimPrefix(header.Name, "./")]
if updateFile {
// this is our file, unpack it
buf := bytes.NewBuffer([]byte{})
if _, copyErr := io.Copy(buf, tarf); copyErr != nil {
return chkitErrors.Wrap(ErrUnpack, copyErr)
}
retVal.Field(field).Set(reflect.ValueOf(io.Reader(buf)))
}
}
}
return nil
}