-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
176 lines (160 loc) · 3.79 KB
/
writer.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) 2020 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package tar
import (
"archive/tar"
"compress/gzip"
"io"
"os"
"path/filepath"
"time"
)
// Writer provides sequential writing of a tar archive.
type Writer struct {
*tar.Writer
gw *gzip.Writer
f *os.File
}
// NewWriter creates a new Writer writing to w.
func NewWriter(w io.Writer) *Writer {
return &Writer{Writer: tar.NewWriter(w)}
}
// NewGzipWriter creates a new gzip Writer writing to w.
func NewGzipWriter(w io.Writer) *Writer {
gw := gzip.NewWriter(w)
return &Writer{Writer: tar.NewWriter(gw), gw: gw}
}
// NewFileWriter creates a new Writer writing to file.
func NewFileWriter(name string) (*Writer, error) {
f, err := os.Create(name)
if err != nil {
return nil, err
}
return &Writer{Writer: tar.NewWriter(f), f: f}, nil
}
// NewGzipFileWriter creates a new gzip Writer writing to file.
func NewGzipFileWriter(name string) (*Writer, error) {
f, err := os.Create(name)
if err != nil {
return nil, err
}
gw := gzip.NewWriter(f)
return &Writer{Writer: tar.NewWriter(gw), gw: gw, f: f}, nil
}
// Flush finishes writing the current file's block padding.
// The current file must be fully written before Flush can be called.
//
// This is unnecessary as the next call to WriteHeader or Close
// will implicitly flush out the file's padding.
func (w *Writer) Flush() error {
err := w.Writer.Flush()
if err != nil {
return err
}
if w.gw != nil {
return w.gw.Flush()
}
return nil
}
// Tar tars all the paths to the tar file.
func (w *Writer) Tar(paths ...string) error {
for _, name := range paths {
if err := w.tarPath(name); err != nil {
return err
}
}
return nil
}
func (w *Writer) tarPath(name string) error {
info, err := os.Stat(name)
if err == nil {
if info.IsDir() {
err = w.TarDir(name)
} else {
err = w.tarFile(name, "", info)
}
}
return err
}
// TarDir tars a dir to the tar file.
func (w *Writer) TarDir(dir string) error {
var base = filepath.Base(dir)
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err == nil {
var rel string
rel, err = filepath.Rel(dir, path)
if err == nil {
if info.IsDir() {
return w.tarDir(path, filepath.Join(base, rel), info)
}
err = w.tarFile(path, filepath.Join(base, rel), info)
}
}
return err
})
}
// TarFile writes a file to the tar file.
func (w *Writer) TarFile(name string) error {
info, err := os.Stat(name)
if err != nil {
return err
}
return w.tarFile(name, "", info)
}
func (w *Writer) tarDir(path, name string, info os.FileInfo) (err error) {
var hdr *tar.Header
hdr, err = tar.FileInfoHeader(info, "")
if err == nil {
if len(name) > 0 {
hdr.Name = name
}
err = w.WriteHeader(hdr)
}
return err
}
func (w *Writer) tarFile(path, name string, info os.FileInfo) (err error) {
var hdr *tar.Header
hdr, err = tar.FileInfoHeader(info, "")
if err == nil {
if len(name) > 0 {
hdr.Name = name
}
err = w.WriteHeader(hdr)
if err == nil {
var f *os.File
f, err = os.Open(path)
if err == nil {
_, err = io.Copy(w, f)
f.Close()
}
}
}
return err
}
// TarBytes tars a file with the file name and data.
func (w *Writer) TarBytes(name string, data []byte) (err error) {
hdr := &tar.Header{
Name: name,
Size: int64(len(data)),
Mode: 0666,
ModTime: time.Now(),
}
err = w.WriteHeader(hdr)
if err == nil {
_, err = w.Write(data)
}
return err
}
// Close closes the tar archive by flushing the padding, and writing the footer.
// If the current file (from a prior call to WriteHeader) is not fully written,
// then this returns an error.
func (w *Writer) Close() error {
err := w.Writer.Close()
if w.gw != nil {
w.gw.Close()
}
if w.f != nil {
w.f.Close()
}
return err
}