| 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 |
|
| 14 |
w, err := ioutil.TempFile("", "zipextra-example") |
| 15 |
if err != nil { |
| 16 |
panic(err) |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
zw := zip.NewWriter(w) |
| 21 |
|
| 22 |
|
| 23 |
fh := &zip.FileHeader{Name: "test_file.txt"} |
| 24 |
|
| 25 |
|
| 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 |
|
| 30 |
fw, err := zw.CreateHeader(fh) |
| 31 |
if err != nil { |
| 32 |
panic(err) |
| 33 |
} |
| 34 |
fw.Write([]byte("foobar")) |
| 35 |
zw.Close() |
| 36 |
|
| 37 |
|
| 38 |
zr, err := zip.OpenReader(w.Name()) |
| 39 |
if err != nil { |
| 40 |
panic(err) |
| 41 |
} |
| 42 |
defer zr.Close() |
| 43 |
|
| 44 |
|
| 45 |
fields, err := zipextra.Parse(zr.File[0].Extra) |
| 46 |
if err != nil { |
| 47 |
panic(err) |
| 48 |
} |
| 49 |
|
| 50 |
|
| 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 |
|
| 66 |
|
| 67 |
世界 |
| 68 |
} |