-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathexample_test.go
80 lines (71 loc) · 1.55 KB
/
example_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
package siva_test
import (
"bytes"
"fmt"
"io"
"log"
"os"
"time"
"gopkg.in/src-d/go-siva.v1"
)
func Example() {
// Create a buffer to write our archive to.
buf := new(bytes.Buffer)
// Create a new siva archive.
w := siva.NewWriter(buf)
// Add some files to the archive.
var files = []struct {
Name, Body string
}{
{"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
{"readme.txt", "This archive contains some text files."},
{"todo.txt", "Get animal handling license."},
}
for _, file := range files {
hdr := &siva.Header{
Name: file.Name,
Mode: 0600,
ModTime: time.Now(),
}
if err := w.WriteHeader(hdr); err != nil {
log.Fatalln(err)
}
if _, err := w.Write([]byte(file.Body)); err != nil {
log.Fatalln(err)
}
}
// Make sure to check the error on Close.
if err := w.Close(); err != nil {
log.Fatalln(err)
}
// Open the siva archive for reading.
file := bytes.NewReader(buf.Bytes())
r := siva.NewReader(file)
// Get all files in the siva file.
i, err := r.Index()
if err != nil {
log.Fatalln(err)
}
// Iterate through the files in the archive.
for _, e := range i {
content, err := r.Get(e)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("Contents of %s:\n", e.Name)
if _, err := io.Copy(os.Stdout, content); err != nil {
log.Fatalln(err)
}
fmt.Println()
}
// Output:
// Contents of gopher.txt:
// Gopher names:
// George
// Geoffrey
// Gonzo
// Contents of readme.txt:
// This archive contains some text files.
// Contents of todo.txt:
// Get animal handling license.
}