-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzip.go
46 lines (43 loc) · 1.01 KB
/
gzip.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
package targz
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
)
func check_err(err error, msg string) {
if err != nil {
fmt.Println("[Failure] ", msg)
os.Exit(-1)
} else {
fmt.Println("[Success] ", msg)
}
}
func Untargz(gzipstream io.Reader) {
fmt.Println("UntarGz in progress...")
uncompressed, err := gzip.NewReader(gzipstream)
check_err(err, "uncompression")
tarstream := tar.NewReader(uncompressed)
for true {
header, err := tarstream.Next()
if err == io.EOF {
break
}
check_err(err, "iteration on files")
switch header.Typeflag {
case tar.TypeDir:
err := os.Mkdir(header.Name, 0755)
check_err(err, "folder creation")
case tar.TypeReg:
outFile, err := os.Create(header.Name)
check_err(err, "file creation")
defer outFile.Close()
bytes, err := io.Copy(outFile, tarstream)
check_err(err, "copy")
fmt.Println(bytes, " bytes written in : ", header.Name)
default:
fmt.Println("Error : unknown type: %s in %s", header.Typeflag, header.Name)
}
}
}