-
Notifications
You must be signed in to change notification settings - Fork 132
/
io.go
68 lines (57 loc) · 1.32 KB
/
io.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
package utils
import (
"archive/zip"
"bytes"
"io"
"io/ioutil"
"strings"
)
type Zip interface {
GetFileFromZip(zipFile string, filename string) (string, error)
}
type Zipper struct {
}
func (z *Zipper) GetFileFromZip(zipFile string, filename string) (string, error) {
reader, err := zip.OpenReader(zipFile)
if err != nil {
return "", err
}
defer reader.Close()
for _, file := range reader.File {
if file.FileInfo().IsDir() {
continue
}
if strings.HasSuffix(filename, file.Name) {
rc, err := file.Open()
if err != nil {
return "", err
}
defer rc.Close()
// Create a buffer to write our archive to.
buf := new(bytes.Buffer)
_, err = io.Copy(buf, rc)
if err != nil {
return "", err
}
// Create a temporary file within our temp-images directory that follows
// a particular naming pattern
tempFile, err := ioutil.TempFile("", "digger-*.tfplan")
if err != nil {
return "", err
}
defer tempFile.Close()
// Read all of the contents of our archive into a byte array.
contents, err := ioutil.ReadAll(buf)
if err != nil {
return "", err
}
// Write the contents of the archive, from the byte array, to our temporary file.
_, err = tempFile.Write(contents)
if err != nil {
return "", err
}
return tempFile.Name(), nil
}
}
return "", nil
}