| 1 | # zipextra |
| 2 | |
| 3 | [](http://godoc.org/github.com/saracen/zipextra) |
| 4 | |
| 5 | `zipextra` is a library for encoding and decoding ZIP archive format's |
| 6 | "Extra Fields". |
| 7 | |
| 8 | The intention is to eventually support and provide a low-level API for the |
| 9 | majority of PKWARE's and Info-ZIP's extra fields. |
| 10 | |
| 11 | Contributions are welcome. |
| 12 | |
| 13 | ## Supported Fields |
| 14 | |
| 15 | | Identifier | Name | |
| 16 | | ---------- | -------------------------- | |
| 17 | | `0x000a` | NTFS | |
| 18 | | `0x6375` | Info-ZIP's Unicode Comment | |
| 19 | | `0x7875` | Info-ZIP's New Unix | |
| 20 | |
| 21 | ### Example |
| 22 | |
| 23 | ``` |
| 24 | func ExampleZipExtra() { |
| 25 | // create temporary file |
| 26 | w, err := ioutil.TempFile("", "zipextra-example") |
| 27 | if err != nil { |
| 28 | panic(err) |
| 29 | } |
| 30 | |
| 31 | // create new zip writer |
| 32 | zw := zip.NewWriter(w) |
| 33 | |
| 34 | // create a new zip file header |
| 35 | fh := &zip.FileHeader{Name: "test_file.txt"} |
| 36 | |
| 37 | // add some extra fields |
| 38 | fh.Extra = append(fh.Extra, zipextra.NewInfoZIPNewUnix(big.NewInt(1000), big.NewInt(1000)).Encode()...) |
| 39 | fh.Extra = append(fh.Extra, zipextra.NewInfoZIPUnicodeComment("Hello, 世界").Encode()...) |
| 40 | |
| 41 | // create the file |
| 42 | fw, err := zw.CreateHeader(fh) |
| 43 | if err != nil { |
| 44 | panic(err) |
| 45 | } |
| 46 | fw.Write([]byte("foobar")) |
| 47 | zw.Close() |
| 48 | |
| 49 | // open the newly created zip |
| 50 | zr, err := zip.OpenReader(w.Name()) |
| 51 | if err != nil { |
| 52 | panic(err) |
| 53 | } |
| 54 | defer zr.Close() |
| 55 | |
| 56 | // parse extra fields |
| 57 | fields, err := zipextra.Parse(zr.File[0].Extra) |
| 58 | if err != nil { |
| 59 | panic(err) |
| 60 | } |
| 61 | |
| 62 | // print extra field information |
| 63 | for id, field := range fields { |
| 64 | switch id { |
| 65 | case zipextra.ExtraFieldUnixN: |
| 66 | unix, _ := field.InfoZIPNewUnix() |
| 67 | fmt.Printf("UID: %d, GID: %d\n", unix.Uid, unix.Gid) |
| 68 | |
| 69 | case zipextra.ExtraFieldUCom: |
| 70 | ucom, _ := field.InfoZIPUnicodeComment() |
| 71 | fmt.Printf("Comment: %s\n", ucom.Comment) |
| 72 | } |
| 73 | } |
| 74 | // Output: |
| 75 | // UID: 1000, GID: 1000 |
| 76 | // Comment: Hello, 世界 |
| 77 | } |
| 78 | ``` |