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
|
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package utils
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
)
// ExtractTarGz takes in an io.Reader containing the bytes for a .tar.gz file and
// a destination string to extract to.
func ExtractTarGz(gzipStream io.Reader, dst string) error {
uncompressedStream, err := gzip.NewReader(gzipStream)
if err != nil {
return fmt.Errorf("ExtractTarGz: NewReader failed: %s", err.Error())
}
defer uncompressedStream.Close()
tarReader := tar.NewReader(uncompressedStream)
for true {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("ExtractTarGz: Next() failed: %s", err.Error())
}
switch header.Typeflag {
case tar.TypeDir:
if PathTraversesUpward(header.Name) {
return fmt.Errorf("ExtractTarGz: path attempts to traverse upwards")
}
path := filepath.Join(dst, header.Name)
if err := os.Mkdir(path, 0744); err != nil && !os.IsExist(err) {
return fmt.Errorf("ExtractTarGz: Mkdir() failed: %s", err.Error())
}
case tar.TypeReg:
if PathTraversesUpward(header.Name) {
return fmt.Errorf("ExtractTarGz: path attempts to traverse upwards")
}
path := filepath.Join(dst, header.Name)
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0744); err != nil {
return fmt.Errorf("ExtractTarGz: MkdirAll() failed: %s", err.Error())
}
outFile, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.FileMode(header.Mode))
if err != nil {
return fmt.Errorf("ExtractTarGz: Create() failed: %s", err.Error())
}
defer outFile.Close()
if _, err := io.Copy(outFile, tarReader); err != nil {
return fmt.Errorf("ExtractTarGz: Copy() failed: %s", err.Error())
}
default:
return fmt.Errorf(
"ExtractTarGz: unknown type: %v in %v",
header.Typeflag,
header.Name)
}
}
return nil
}
|