-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtar.go
91 lines (80 loc) · 1.53 KB
/
tar.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
package main
import (
"archive/tar"
"compress/gzip"
"io"
"log"
"net/http"
"os"
"path/filepath"
"github.com/pkg/errors"
)
func contains(s string, from []string) bool {
for _, i := range from {
if s == i {
return true
}
}
return false
}
func extractURL(url string, out string, skip []string) ([]string, error) {
// f, err := os.Open(srcFile)
// if err != nil {
// return err
// }
// defer f.Close()
if out == "" {
out = "."
}
if err := os.MkdirAll(out, 0755); err != nil {
return nil, err
}
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
reader, err := gzip.NewReader(response.Body)
if err != nil {
return nil, err
}
tarReader := tar.NewReader(reader)
files := []string{}
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
switch header.Typeflag {
case tar.TypeDir:
if contains(header.Name, skip) {
continue
}
if err := os.Mkdir(header.Name, 0755); err != nil {
return nil, err
}
case tar.TypeReg:
p := filepath.Join(out, header.Name)
if contains(header.Name, skip) {
continue
}
files = append(files, p)
log.Printf("%s\n", p)
outFile, err := os.Create(p)
if err != nil {
return nil, err
}
defer outFile.Close()
if _, err := io.Copy(outFile, tarReader); err != nil {
return nil, err
}
outFile.Close()
default:
return nil, errors.Errorf("unknown type: %b in %s", header.Typeflag, header.Name)
}
}
return files, nil
}