-
Notifications
You must be signed in to change notification settings - Fork 51
/
untar.go
80 lines (66 loc) · 1.83 KB
/
untar.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
// Copyright (c) Edgeless Systems GmbH.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package launch
import (
"archive/tar"
"compress/gzip"
"io"
"os"
"path/filepath"
"github.com/spf13/afero"
)
// UntarGzip unpacks a gzip-packed tar archive to the given path.
// Adapted from: https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07
func UntarGzip(fs afero.Fs, r io.Reader, dst string) error {
// Extract the runtime to a temporary directory
compressedReader, err := gzip.NewReader(r)
if err != nil {
return err
}
// Extract the tarball to the temporary directory
tarReader := tar.NewReader(compressedReader)
for {
header, err := tarReader.Next()
// When done with the archive, stop.
if err == io.EOF {
break
}
if err != nil {
return err
}
if header == nil {
continue
}
// Construct path from the entry in the tarball
target := filepath.Join(dst, header.Name)
switch header.Typeflag {
// Directory
case tar.TypeDir:
if err := fs.MkdirAll(target, 0o755); err != nil {
return err
}
// File
case tar.TypeReg:
// Tar files do not always have to have a TypeDir object in the header to specify a directory.
// They can also be referred to implcitly by a filename with dashes.
// Therefore, we also call MkdirAll here to create all upper directories.
if err := fs.MkdirAll(filepath.Dir(target), 0o755); err != nil {
return err
}
fsFile, err := fs.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}
if _, err := io.Copy(fsFile, tarReader); err != nil {
return err
}
if err := fsFile.Close(); err != nil {
return err
}
}
}
return nil
}