zipextra

package module
v0.0.0-...-0187cb0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2022 License: MIT Imports: 5 Imported by: 2

README

zipextra

zipextra is a library for encoding and decoding ZIP archive format's "Extra Fields".

The intention is to eventually support and provide a low-level API for the majority of PKWARE's and Info-ZIP's extra fields.

Contributions are welcome.

Supported Fields

Identifier Name
0x000a NTFS
0x6375 Info-ZIP's Unicode Comment
0x7875 Info-ZIP's New Unix
Example
func ExampleZipExtra() {
	// create temporary file
	w, err := ioutil.TempFile("", "zipextra-example")
	if err != nil {
		panic(err)
	}

	// create new zip writer
	zw := zip.NewWriter(w)

	// create a new zip file header
	fh := &zip.FileHeader{Name: "test_file.txt"}

	// add some extra fields
	fh.Extra = append(fh.Extra, zipextra.NewInfoZIPNewUnix(big.NewInt(1000), big.NewInt(1000)).Encode()...)
	fh.Extra = append(fh.Extra, zipextra.NewInfoZIPUnicodeComment("Hello, 世界").Encode()...)

	// create the file
	fw, err := zw.CreateHeader(fh)
	if err != nil {
		panic(err)
	}
	fw.Write([]byte("foobar"))
	zw.Close()

	// open the newly created zip
	zr, err := zip.OpenReader(w.Name())
	if err != nil {
		panic(err)
	}
	defer zr.Close()

	// parse extra fields
	fields, err := zipextra.Parse(zr.File[0].Extra)
	if err != nil {
		panic(err)
	}

	// print extra field information
	for id, field := range fields {
		switch id {
		case zipextra.ExtraFieldUnixN:
			unix, _ := field.InfoZIPNewUnix()
			fmt.Printf("UID: %d, GID: %d\n", unix.Uid, unix.Gid)

		case zipextra.ExtraFieldUCom:
			ucom, _ := field.InfoZIPUnicodeComment()
			fmt.Printf("Comment: %s\n", ucom.Comment)
		}
	}
	// Output:
	// UID: 1000, GID: 1000
	// Comment: Hello, 世界
}

Documentation

Index

Constants

View Source
const ExtraFieldExtTime uint16 = 0x5455

ExtraFieldExtTime is the Extended Timestamp Extra Field identifier.

View Source
const ExtraFieldNTFS uint16 = 0x000a

NTFS Extra Field identifier

View Source
const ExtraFieldUCom uint16 = 0x6375

ExtraFieldUCom is the Info-ZIP's Unicode Comment Extra Field identifier.

View Source
const ExtraFieldUnixN uint16 = 0x7875

ExtraFieldUnixN is the Info-ZIP's New Unix (3rd generation) Extra Field identifier.

Variables

View Source
var ErrInvalidExtraFieldFormat = errors.New("invalid extra field format")

ErrInvalidExtraFieldFormat is returned when the extra field's format is incorrect.

Functions

func Parse

func Parse(extra []byte) (map[uint16]ExtraField, error)

Parse parses all extra fields and returns a map with extra field identifiers as keys.

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer provides primitive read/write functions for extra fields.

func NewBuffer

func NewBuffer(e []byte) *Buffer

NewBuffer returns a new Buffer.

func (*Buffer) Available

func (p *Buffer) Available() int

Available returns the number of unread bytes available.

func (*Buffer) Bytes

func (p *Buffer) Bytes() []byte

Bytes returns the unread bytes.

func (*Buffer) Read16

func (p *Buffer) Read16() (n uint16)

Read16 reads and returns a uint16.

func (*Buffer) Read32

func (p *Buffer) Read32() (n uint32)

Read32 reads and returns a uint32.

func (*Buffer) Read8

func (p *Buffer) Read8() (n uint8)

Read8 reads and returns a uint8.

func (*Buffer) ReadBytes

func (p *Buffer) ReadBytes(size int) (b []byte)

ReadBytes reads the specified number of bytes.

func (*Buffer) Skip

func (p *Buffer) Skip(n int)

Skip skips n bytes.

func (*Buffer) Write16

func (p *Buffer) Write16(n uint16)

Write8 writes a uint16.

func (*Buffer) Write32

func (p *Buffer) Write32(n uint32)

Write8 writes a uint32.

func (*Buffer) Write8

func (p *Buffer) Write8(n uint8)

Write8 writes a uint8.

func (*Buffer) WriteByte

func (p *Buffer) WriteByte(b byte)

WriteByte writes a single byte.

func (*Buffer) WriteBytes

func (p *Buffer) WriteBytes(b []byte)

WriteBytes writes multiple bytes.

func (*Buffer) WriteHeader

func (p *Buffer) WriteHeader(id uint16) func()

WriteHeader writes a standard Zip Extra Field header, consisting of a uint16 identifier, and uint16 size.

type ExtendedTimestamp

type ExtendedTimestamp struct {
	ModTime time.Time
}

ExtendedTime is the Extended Timestamp Extra Field structure for holding a file entry's last modification time.

func NewExtendedTimestamp

func NewExtendedTimestamp(modTime time.Time) ExtendedTimestamp

NewExtendedTimestamp returns a new ExtendedTime extra field structure.

func (ExtendedTimestamp) Encode

func (field ExtendedTimestamp) Encode() []byte

Encode encodes the ExtendedTimestamp extra field.

type ExtraField

type ExtraField []byte

ExtraField is a ZIP Extra Field byte slice.

func (ExtraField) ExtendedTimestamp

func (ef ExtraField) ExtendedTimestamp() (field ExtendedTimestamp, err error)

ExtendedTime returns the decoded ExtendedTime extra field.

func (ExtraField) InfoZIPNewUnix

func (ef ExtraField) InfoZIPNewUnix() (field InfoZIPNewUnix, err error)

InfoZIPNewUnix returns the decoded InfoZIPNewUnix extra field.

func (ExtraField) InfoZIPUnicodeComment

func (ef ExtraField) InfoZIPUnicodeComment() (field InfoZIPUnicodeComment, err error)

InfoZIPUnicodeComment returns the decoded InfoZIPUnicodeComment extra field.

func (ExtraField) NTFS

func (ef ExtraField) NTFS() (field NTFS, err error)

NTFS returns the decoded NTFS extra field.

type InfoZIPNewUnix

type InfoZIPNewUnix struct {
	Version uint8
	Uid     *big.Int
	Gid     *big.Int
}

InfoZIPNewUnix is the New Unix Extra Field structure for holding UID and GID file ownership data.

func NewInfoZIPNewUnix

func NewInfoZIPNewUnix(uid *big.Int, gid *big.Int) InfoZIPNewUnix

NewInfoZIPNewUnix returns a new InfoZIPNewUnix extra field structure.

func (InfoZIPNewUnix) Encode

func (field InfoZIPNewUnix) Encode() []byte

Encode encodes the InfoZIPNewUnix extra field.

type InfoZIPUnicodeComment

type InfoZIPUnicodeComment struct {
	Version uint8
	Crc32   uint32
	Comment string
}

InfoZIPUnicodeComment is the Unicode Comment Extra Field structure for holding NTFS file comments.

func NewInfoZIPUnicodeComment

func NewInfoZIPUnicodeComment(comment string) InfoZIPUnicodeComment

NewInfoZIPUnicodeComment returns a new InfoZIPUnicodeComment extra field structure.

func (InfoZIPUnicodeComment) Encode

func (field InfoZIPUnicodeComment) Encode() []byte

Encode encodes the InfoZIPUnicodeComment extra field.

type NTFS

type NTFS struct {
	Attributes []NTFSAttribute
}

NTFS is the NTFS Extra Field structure for holding NTFS attributes.

func NewNTFS

func NewNTFS(attributes ...NTFSAttribute) NTFS

NewNTFS returns a new NTFS extra field structure.

func (NTFS) Encode

func (field NTFS) Encode() []byte

Encode encodes the NTFS extra field.

type NTFSAttribute

type NTFSAttribute interface {
	Tag() uint16
	Data() []byte
}

NTFSAttribute is an NTFS attribute interface. Each attribute consists of a tag and data. The tag is an identifier for the type of data.

type NTFSRawAttribute

type NTFSRawAttribute struct {
	RawTag  uint16
	RawData []byte
}

NTFSRawAttribute represents an unrecognized NTFS attribute.

func (NTFSRawAttribute) Data

func (a NTFSRawAttribute) Data() []byte

Tag returns the unrecognized NTFS attribute's data.

func (NTFSRawAttribute) Tag

func (a NTFSRawAttribute) Tag() uint16

Tag returns the unrecognized NTFS attribute's tag.

type NTFSTimeAttribute

type NTFSTimeAttribute struct {
	MTime time.Time
	ATime time.Time
	CTime time.Time
}

NTFSTimeAttribute is an NTFS attribute for storing modified, access and creation file times.

func (NTFSTimeAttribute) Data

func (a NTFSTimeAttribute) Data() []byte

Data returns the NTFS Time attribute's encoded data.

func (NTFSTimeAttribute) Tag

func (a NTFSTimeAttribute) Tag() uint16

Tag returns the NTFS Time attribute's tag. It is always 1.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL