This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.go
146 lines (120 loc) · 2.85 KB
/
archive.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"context"
"fmt"
"io"
"os"
"path/filepath"
)
var errCancelled = fmt.Errorf("request cancelled")
// GenerateZIPFromDir generates a zip file from the given directory
func GenerateZIPFromDir(to io.Writer, directory string, ctx context.Context) (err error) {
zipW := zip.NewWriter(to)
err = filepath.Walk(directory, func(path string, f os.FileInfo, err error) error {
select {
case <-ctx.Done():
return errCancelled
default:
}
if err != nil || f.IsDir() {
return err
}
// Get relative path for zip file
relPath, err := filepath.Rel(directory, path)
if err != nil {
return err
}
// Get the file header that contains all info for the zip file (dates...)
fh, err := zip.FileInfoHeader(f)
if err != nil {
return err
}
fh.Name = relPath
// Open the actual file
diskFile, err := os.Open(path)
if err != nil {
return err
}
defer diskFile.Close()
// Create the header in the zip file
fw, err := zipW.CreateHeader(fh)
if err != nil {
return err
}
// Copy the file content to the zip file
_, err = io.Copy(fw, diskFile)
return err
})
if err != nil {
return err
}
// Finalize the zip file
if err = zipW.Close(); err != nil {
return err
}
return err
}
// GenerateTARFromDir generates a tar file from the given directory
func GenerateTARFromDir(to io.Writer, directory string, ctx context.Context) (err error) {
tarW := tar.NewWriter(to)
err = filepath.Walk(directory, func(path string, f os.FileInfo, err error) error {
select {
case <-ctx.Done():
return errCancelled
default:
}
if err != nil || f.IsDir() {
return err
}
// Get relative path for tar file
relPath, err := filepath.Rel(directory, path)
if err != nil {
return err
}
// Get the file header that contains all info for the tar file (dates...)
fh, err := tar.FileInfoHeader(f, "")
if err != nil {
return err
}
fh.Name = relPath
// Open the actual file
diskFile, err := os.Open(path)
if err != nil {
return err
}
defer diskFile.Close()
// Write the header in the tar file
err = tarW.WriteHeader(fh)
if err != nil {
return err
}
// Copy the file content to the tar file
_, err = io.Copy(tarW, diskFile)
return err
})
if err != nil {
return err
}
// Finalize the tar file
if err = tarW.Close(); err != nil {
return err
}
return err
}
// GenerateTARGZFromDir generates a tar.gz file from the given directory
func GenerateTARGZFromDir(to io.Writer, directory string, ctx context.Context) (err error) {
w, err := gzip.NewWriterLevel(to, gzip.BestCompression) // I mean, if we want the compressed version then we probably want the best compressed version
if err != nil {
return
}
defer func() {
cerr := w.Close()
if err == nil {
err = cerr
}
}()
return GenerateTARFromDir(w, directory, ctx)
}