Debian Codegraph @

zipextra_example_test.go

raw symbols SCIP
1 package zipextra_test
2
3 import (
4 "archive/zip"
5 "fmt"
6 "io/ioutil"
7 "math/big"
8
9 "github.com/saracen/zipextra"
10 )
11
12 func ExampleZipExtra() {
13 // create temporary file
14 w, err := ioutil.TempFile("", "zipextra-example")
15 if err != nil {
16 panic(err)
17 }
18
19 // create new zip writer
20 zw := zip.NewWriter(w)
21
22 // create a new zip file header
23 fh := &zip.FileHeader{Name: "test_file.txt"}
24
25 // add some extra fields
26 fh.Extra = append(fh.Extra, zipextra.NewInfoZIPNewUnix(big.NewInt(1000), big.NewInt(1000)).Encode()...)
27 fh.Extra = append(fh.Extra, zipextra.NewInfoZIPUnicodeComment("Hello, 世界").Encode()...)
28
29 // create the file
30 fw, err := zw.CreateHeader(fh)
31 if err != nil {
32 panic(err)
33 }
34 fw.Write([]byte("foobar"))
35 zw.Close()
36
37 // open the newly created zip
38 zr, err := zip.OpenReader(w.Name())
39 if err != nil {
40 panic(err)
41 }
42 defer zr.Close()
43
44 // parse extra fields
45 fields, err := zipextra.Parse(zr.File[0].Extra)
46 if err != nil {
47 panic(err)
48 }
49
50 // print extra field information
51 for id, field := range fields {
52 switch id {
53 case zipextra.ExtraFieldUnixN:
54 unix, _ := field.InfoZIPNewUnix()
55 fmt.Printf("UID: %d, GID: %d\n", unix.Uid, unix.Gid)
56 }
57 }
58 for id, field := range fields {
59 switch id {
60 case zipextra.ExtraFieldUCom:
61 ucom, _ := field.InfoZIPUnicodeComment()
62 fmt.Printf("Comment: %s\n", ucom.Comment)
63 }
64 }
65 // Output:
66 // UID: 1000, GID: 1000
67 // Comment: Hello, 世界
68 }