forked from mholt/archiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc_test.go
260 lines (235 loc) · 7.24 KB
/
doc_test.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package archiver
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
)
// The simplest use of this package: create an archive file
// from a list of filenames. This is the recommended way to
// do so using a default configuration, as it guarantees
// the file format matches the file extension, because the
// format to write is determined by the given extension.
func ExampleArchive() {
// any files in this list are added
// to the top level of the archive;
// directories are recursively added
files := []string{
"index.html",
"photo.jpg",
"blog", // directory
"/home/website/copyright.txt",
}
// archive format is determined by file extension
err := Archive(files, "blog_site.zip")
if err != nil {
log.Fatal(err)
}
}
// The simplest use of this package: extract all of an archive's
// contents to a folder on disk using the default configuration.
// The archive format is determined automatically.
func ExampleUnarchive() {
err := Unarchive("blog_site.zip", "extracted/mysite")
if err != nil {
log.Fatal(err)
}
}
// In this example, the DefaultZip is being customized so that
// all calls to its methods will use that configuration.
func ExampleZip_default() {
DefaultZip.OverwriteExisting = true
DefaultZip.ImplicitTopLevelFolder = true
// any subsequent use of DefaultZip uses
// this modified configuration
}
// Here we create our own instance of the Zip format. No need
// to use the constructor function (NewZip) or the default
// instance (DefaultZip) if we do not want to. Instantiating
// the type like this allows us to easily be very explicit
// about our configuration.
func ExampleZip_custom() {
z := &Zip{
CompressionLevel: 3,
OverwriteExisting: false,
MkdirAll: true,
SelectiveCompression: true,
ImplicitTopLevelFolder: true,
ContinueOnError: false,
}
// z is now ready to use for whatever (this is a dumb example)
fmt.Println(z.CheckExt("test.zip"))
}
// Much like the package-level Archive function, this creates an
// archive using the configuration of the Zip instance it is called
// on. The output filename must match the format's recognized file
// extension(s).
func ExampleZip_Archive() {
err := DefaultZip.Archive([]string{"..."}, "example.zip")
if err != nil {
log.Fatal(err)
}
}
// It's easy to list the items in an archive. This example
// prints the name and size of each file in the archive. Like
// other top-level functions in this package, the format is
// inferred automatically for you.
func ExampleWalk() {
err := Walk("example.tar.gz", func(f File) error {
fmt.Println(f.Name(), f.Size())
// you could also read the contents; f is an io.Reader!
return nil
})
if err != nil {
log.Fatal(err)
}
}
// This example extracts target.txt from inside example.rar
// and puts it into a folder on disk called output/dir.
func ExampleExtract() {
err := Extract("example.rar", "target.txt", "output/dir")
if err != nil {
log.Fatal(err)
}
}
// This example demonstrates how to read an
// archive in a streaming fashion. The idea
// is that you can stream the bytes of an
// archive from a stream, regardless of
// whether it is an actual file on disk.
// This means that you can read a huge
// archive file-by-file rather than having
// to store it all on disk first. In this
// example, we read a hypothetical archive
// from a (fake) HTTP request body and
// print its file names and sizes. The
// files can be read, of course, but they
// do not have to be.
func ExampleZip_streamingRead() {
// for the sake of the example compiling, pretend we have an HTTP request
req := new(http.Request)
contentLen, err := strconv.Atoi(req.Header.Get("Content-Length"))
if err != nil {
log.Fatal(err)
}
// the Zip format requires knowing the length of the stream,
// but other formats don't generally require it, so it
// could be left as 0 when using those
err = DefaultZip.Open(req.Body, int64(contentLen))
if err != nil {
log.Fatal(err)
}
defer DefaultZip.Close()
// Note that DefaultZip now contains some state that
// is critical to reading the stream until it is closed,
// so do not reuse it until then.
// iterate each file in the archive until EOF
for {
f, err := DefaultZip.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
// f is an io.ReadCloser, so you can read its contents
// if you wish; or you can access its header info through
// f.Header or the embedded os.FileInfo
fmt.Println("File name:", f.Name(), "File size:", f.Size())
// be sure to close f before moving on!!
err = f.Close()
if err != nil {
log.Fatal(err)
}
}
}
// This example demonstrates how to write an
// archive in a streaming fashion. The idea
// is that you can stream the bytes of a new
// archive that is created on-the-fly from
// generic streams. Those streams could be
// actual files on disk, or they could be over
// a network, or standard output, or any other
// io.Reader/io.Writer. This example only adds
// one file to the archive and writes the
// resulting archive to standard output, but you
// could add as many files as needed with a loop.
func ExampleZip_streamingWrite() {
err := DefaultZip.Create(os.Stdout)
if err != nil {
log.Fatal(err)
}
defer DefaultZip.Close()
// Note that DefaultZip now contains state
// critical to a successful write until it
// is closed, so don't reuse it for anything
// else until then.
// At this point, you can open an actual file
// to add to the archive, or the "file" could
// come from any io.ReadCloser stream. If you
// only have an io.Reader, you can use
// ReadFakeCloser to make it into an
// io.ReadCloser.
// The next part is a little tricky if you
// don't have an actual file because you will
// need an os.FileInfo. Fortunately, that's an
// interface! So go ahead and implement it in
// whatever way makes the most sense to you.
// You'll also need to give the file a name
// for within the archive. In this example,
// we'll open a real file.
file, err := os.Open("foo.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
log.Fatal(err)
}
err = DefaultZip.Write(File{
FileInfo: FileInfo{
FileInfo: fileInfo,
CustomName: "name/in/archive.txt",
},
ReadCloser: file, // does not have to be an actual file
})
if err != nil {
log.Fatal(err)
}
}
// This example compresses a standard tar file into a tar.gz file.
// Compression formats are selected by file extension.
func ExampleCompressFile() {
err := CompressFile("example.tar", "example.tar.gz")
if err != nil {
log.Fatal(err)
}
}
// This example changes the default configuration for
// the Gz compression format.
func ExampleCompressFile_custom() {
DefaultGz.CompressionLevel = 5
// any calls to DefaultGz now use the modified configuration
}
// This example creates a new Gz instance and
// uses it to compress a stream, writing to
// another stream. This is sometimes preferable
// over modifying the DefaultGz.
func ExampleGz_Compress_custom() {
gz := &Gz{CompressionLevel: 5}
err := gz.Compress(os.Stdin, os.Stdout)
if err != nil {
log.Fatal(err)
}
}
// This example decompresses a gzipped tarball and writes
// it to an adjacent file.
func ExampleDecompressFile() {
err := DecompressFile("example.tar.gz", "example.tar")
if err != nil {
log.Fatal(err)
}
}