1
2
3
4
5 package gzip_test
6
7 import (
8 "bytes"
9 "compress/gzip"
10 "fmt"
11 "io"
12 "log"
13 "os"
14 "time"
15 )
16
17 func Example_writerReader() {
18 var buf bytes.Buffer
19 zw := gzip.NewWriter(&buf)
20
21
22 zw.Name = "a-new-hope.txt"
23 zw.Comment = "an epic space opera by George Lucas"
24 zw.ModTime = time.Date(1977, time.May, 25, 0, 0, 0, 0, time.UTC)
25
26 _, err := zw.Write([]byte("A long time ago in a galaxy far, far away..."))
27 if err != nil {
28 log.Fatal(err)
29 }
30
31 if err := zw.Close(); err != nil {
32 log.Fatal(err)
33 }
34
35 zr, err := gzip.NewReader(&buf)
36 if err != nil {
37 log.Fatal(err)
38 }
39
40 fmt.Printf("Name: %s\nComment: %s\nModTime: %s\n\n", zr.Name, zr.Comment, zr.ModTime.UTC())
41
42 if _, err := io.Copy(os.Stdout, zr); err != nil {
43 log.Fatal(err)
44 }
45
46 if err := zr.Close(); err != nil {
47 log.Fatal(err)
48 }
49
50
51
52
53
54
55
56 }
57
58 func ExampleReader_Multistream() {
59 var buf bytes.Buffer
60 zw := gzip.NewWriter(&buf)
61
62 var files = []struct {
63 name string
64 comment string
65 modTime time.Time
66 data string
67 }{
68 {"file-1.txt", "file-header-1", time.Date(2006, time.February, 1, 3, 4, 5, 0, time.UTC), "Hello Gophers - 1"},
69 {"file-2.txt", "file-header-2", time.Date(2007, time.March, 2, 4, 5, 6, 1, time.UTC), "Hello Gophers - 2"},
70 }
71
72 for _, file := range files {
73 zw.Name = file.name
74 zw.Comment = file.comment
75 zw.ModTime = file.modTime
76
77 if _, err := zw.Write([]byte(file.data)); err != nil {
78 log.Fatal(err)
79 }
80
81 if err := zw.Close(); err != nil {
82 log.Fatal(err)
83 }
84
85 zw.Reset(&buf)
86 }
87
88 zr, err := gzip.NewReader(&buf)
89 if err != nil {
90 log.Fatal(err)
91 }
92
93 for {
94 zr.Multistream(false)
95 fmt.Printf("Name: %s\nComment: %s\nModTime: %s\n\n", zr.Name, zr.Comment, zr.ModTime.UTC())
96
97 if _, err := io.Copy(os.Stdout, zr); err != nil {
98 log.Fatal(err)
99 }
100
101 fmt.Print("\n\n")
102
103 err = zr.Reset(&buf)
104 if err == io.EOF {
105 break
106 }
107 if err != nil {
108 log.Fatal(err)
109 }
110 }
111
112 if err := zr.Close(); err != nil {
113 log.Fatal(err)
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128 }
129
View as plain text