forked from allora-network/b7s
/
gzip.go
101 lines (83 loc) · 2.2 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
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
92
93
94
95
96
97
98
99
100
101
package fstore
import (
"archive/tar"
"compress/gzip"
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
func (h *FStore) unpackArchive(filename string, destination string) error {
// Use CWD if not specified.
if destination == "" {
destination = "."
}
h.log.Debug().
Str("archive", filename).
Str("destination", destination).
Msg("unpacking gzip archive")
// Create output directory.
err := os.MkdirAll(destination, os.ModePerm)
if err != nil {
return fmt.Errorf("could not create destination directory (dir: %s): %w", destination, err)
}
// Open gzip archive.
f, err := os.Open(filename)
if err != nil {
return fmt.Errorf("could not open gzip archive (file: %s): %w", filename, err)
}
defer f.Close()
// Create reader for compressed data.
reader, err := gzip.NewReader(f)
if err != nil {
return fmt.Errorf("could not create gzip reader: %w", err)
}
defer reader.Close()
tarReader := tar.NewReader(reader)
for {
// Get the next record from the archive.
entry, err := tarReader.Next()
if err != nil {
if !errors.Is(err, io.EOF) {
return fmt.Errorf("could not read archive: %w", err)
}
break
}
typ := entry.Typeflag
h.log.Debug().
Str("archive", filename).
Str("entry", entry.Name).
Str("type", fmt.Sprintf("%d", typ)).
Msg("processing archive entry")
switch typ {
case tar.TypeDir:
// Entry is a directory - create output dir.
dir := filepath.Join(destination, entry.Name)
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
return fmt.Errorf("could not create directory (dir: %s): %w", dir, err)
}
case tar.TypeReg:
// Entry is a file - create file.
file := filepath.Join(destination, entry.Name)
of, err := os.Create(file)
if err != nil {
return fmt.Errorf("could not create file (file: %s): %w", file, err)
}
// Copy file content.
_, err = io.Copy(of, tarReader)
of.Close()
if err != nil {
return fmt.Errorf("could not write file content (file: %s): %w", file, err)
}
default:
return fmt.Errorf("unexpected entry found (name: %s, type: %d)", entry.Name, typ)
}
}
h.log.Debug().
Str("archive", filename).
Str("destination", destination).
Msg("gzip archive unpacked")
return nil
}