| 1 |
package zipextra |
| 2 |
|
| 3 |
import "hash/crc32" |
| 4 |
|
| 5 |
|
| 6 |
const ExtraFieldUCom uint16 = 0x6375 |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
type InfoZIPUnicodeComment struct { |
| 11 |
Version uint8 |
| 12 |
Crc32 uint32 |
| 13 |
Comment string |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
func NewInfoZIPUnicodeComment(comment string) InfoZIPUnicodeComment { |
| 19 |
return InfoZIPUnicodeComment{ |
| 20 |
Version: 1, |
| 21 |
Crc32: crc32.ChecksumIEEE([]byte(comment)), |
| 22 |
Comment: comment, |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
func (field InfoZIPUnicodeComment) Encode() []byte { |
| 28 |
buf := NewBuffer([]byte{}) |
| 29 |
defer buf.WriteHeader(ExtraFieldUCom)() |
| 30 |
|
| 31 |
buf.Write8(field.Version) |
| 32 |
buf.Write32(field.Crc32) |
| 33 |
buf.WriteBytes([]byte(field.Comment)) |
| 34 |
|
| 35 |
return buf.Bytes() |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
func (ef ExtraField) InfoZIPUnicodeComment() (field InfoZIPUnicodeComment, err error) { |
| 40 |
buf := NewBuffer(ef) |
| 41 |
if buf.Available() < 5 { |
| 42 |
return field, ErrInvalidExtraFieldFormat |
| 43 |
} |
| 44 |
|
| 45 |
field.Version = buf.Read8() |
| 46 |
field.Crc32 = buf.Read32() |
| 47 |
field.Comment = string(buf.ReadBytes(buf.Available())) |
| 48 |
|
| 49 |
return |
| 50 |
} |