123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736 |
- // Copyright 2011 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package zip
- import (
- "bufio"
- "encoding/binary"
- "errors"
- "hash"
- "hash/crc32"
- "io"
- "strings"
- "unicode/utf8"
- )
- var (
- errLongName = errors.New("zip: FileHeader.Name too long")
- errLongExtra = errors.New("zip: FileHeader.Extra too long")
- )
- type lastWriter interface {
- Close() error
- Closed() bool
- }
- // Writer implements a zip file writer.
- type Writer struct {
- cw *countWriter
- dir []*header
- last lastWriter
- closed bool
- compressors map[uint16]Compressor
- comment string
- // testHookCloseSizeOffset if non-nil is called with the size
- // of offset of the central directory at Close.
- testHookCloseSizeOffset func(size, offset uint64)
- }
- type header struct {
- *FileHeader
- offset uint64
- }
- // NewWriter returns a new Writer writing a zip file to w.
- func NewWriter(w io.Writer) *Writer {
- return &Writer{cw: &countWriter{w: bufio.NewWriter(w)}}
- }
- // SetOffset sets the offset of the beginning of the zip data within the
- // underlying writer. It should be used when the zip data is appended to an
- // existing file, such as a binary executable.
- // It must be called before any data is written.
- func (w *Writer) SetOffset(n int64) {
- if w.cw.count != 0 {
- panic("zip: SetOffset called after data was written")
- }
- w.cw.count = n
- }
- // Flush flushes any buffered data to the underlying writer.
- // Calling Flush is not normally necessary; calling Close is sufficient.
- func (w *Writer) Flush() error {
- return w.cw.w.(*bufio.Writer).Flush()
- }
- // SetComment sets the end-of-central-directory comment field.
- // It can only be called before Close.
- func (w *Writer) SetComment(comment string) error {
- if len(comment) > uint16max {
- return errors.New("zip: Writer.Comment too long")
- }
- w.comment = comment
- return nil
- }
- // Close finishes writing the zip file by writing the central directory.
- // It does not Close the underlying writer.
- func (w *Writer) Close() error {
- if w.last != nil && !w.last.Closed() {
- if err := w.last.Close(); err != nil {
- return err
- }
- w.last = nil
- }
- if w.closed {
- return errors.New("zip: writer closed twice")
- }
- w.closed = true
- // write central directory
- start := w.cw.count
- for _, h := range w.dir {
- var buf [directoryHeaderLen]byte
- b := writeBuf(buf[:])
- b.uint32(uint32(directoryHeaderSignature))
- b.uint16(h.CreatorVersion)
- b.uint16(h.ReaderVersion)
- b.uint16(h.Flags)
- b.uint16(h.Method)
- b.uint16(h.ModifiedTime)
- b.uint16(h.ModifiedDate)
- b.uint32(h.CRC32)
- if h.isZip64() || h.offset >= uint32max {
- // the file needs a zip64 header. store maxint in both
- // 32 bit size fields (and offset later) to signal that the
- // zip64 extra header should be used.
- b.uint32(uint32max) // compressed size
- b.uint32(uint32max) // uncompressed size
- // append a zip64 extra block to Extra
- var buf [28]byte // 2x uint16 + 3x uint64
- eb := writeBuf(buf[:])
- eb.uint16(zip64ExtraID)
- eb.uint16(24) // size = 3x uint64
- eb.uint64(h.UncompressedSize64)
- eb.uint64(h.CompressedSize64)
- eb.uint64(h.offset)
- h.Extra = append(h.Extra, buf[:]...)
- } else {
- b.uint32(h.CompressedSize)
- b.uint32(h.UncompressedSize)
- }
- b.uint16(uint16(len(h.Name)))
- b.uint16(uint16(len(h.Extra)))
- b.uint16(uint16(len(h.Comment)))
- b = b[4:] // skip disk number start and internal file attr (2x uint16)
- b.uint32(h.ExternalAttrs)
- if h.offset > uint32max {
- b.uint32(uint32max)
- } else {
- b.uint32(uint32(h.offset))
- }
- if _, err := w.cw.Write(buf[:]); err != nil {
- return err
- }
- if _, err := io.WriteString(w.cw, h.Name); err != nil {
- return err
- }
- if _, err := w.cw.Write(h.Extra); err != nil {
- return err
- }
- if _, err := io.WriteString(w.cw, h.Comment); err != nil {
- return err
- }
- }
- end := w.cw.count
- records := uint64(len(w.dir))
- size := uint64(end - start)
- offset := uint64(start)
- if f := w.testHookCloseSizeOffset; f != nil {
- f(size, offset)
- }
- if records >= uint16max || size >= uint32max || offset >= uint32max {
- var buf [directory64EndLen + directory64LocLen]byte
- b := writeBuf(buf[:])
- // zip64 end of central directory record
- b.uint32(directory64EndSignature)
- b.uint64(directory64EndLen - 12) // length minus signature (uint32) and length fields (uint64)
- b.uint16(zipVersion45) // version made by
- b.uint16(zipVersion45) // version needed to extract
- b.uint32(0) // number of this disk
- b.uint32(0) // number of the disk with the start of the central directory
- b.uint64(records) // total number of entries in the central directory on this disk
- b.uint64(records) // total number of entries in the central directory
- b.uint64(size) // size of the central directory
- b.uint64(offset) // offset of start of central directory with respect to the starting disk number
- // zip64 end of central directory locator
- b.uint32(directory64LocSignature)
- b.uint32(0) // number of the disk with the start of the zip64 end of central directory
- b.uint64(uint64(end)) // relative offset of the zip64 end of central directory record
- b.uint32(1) // total number of disks
- if _, err := w.cw.Write(buf[:]); err != nil {
- return err
- }
- // store max values in the regular end record to signal
- // that the zip64 values should be used instead
- records = uint16max
- size = uint32max
- offset = uint32max
- }
- // write end record
- var buf [directoryEndLen]byte
- b := writeBuf(buf[:])
- b.uint32(uint32(directoryEndSignature))
- b = b[4:] // skip over disk number and first disk number (2x uint16)
- b.uint16(uint16(records)) // number of entries this disk
- b.uint16(uint16(records)) // number of entries total
- b.uint32(uint32(size)) // size of directory
- b.uint32(uint32(offset)) // start of directory
- b.uint16(uint16(len(w.comment))) // byte size of EOCD comment
- if _, err := w.cw.Write(buf[:]); err != nil {
- return err
- }
- if _, err := io.WriteString(w.cw, w.comment); err != nil {
- return err
- }
- return w.cw.w.(*bufio.Writer).Flush()
- }
- // Create adds a file to the zip file using the provided name.
- // It returns a Writer to which the file contents should be written.
- // The file contents will be compressed using the Deflate method.
- // The name must be a relative path: it must not start with a drive
- // letter (e.g. C:) or leading slash, and only forward slashes are
- // allowed. To create a directory instead of a file, add a trailing
- // slash to the name.
- // The file's contents must be written to the io.Writer before the next
- // call to Create, CreateHeader, or Close.
- func (w *Writer) Create(name string) (io.Writer, error) {
- header := &FileHeader{
- Name: name,
- Method: Deflate,
- }
- return w.CreateHeader(header)
- }
- // Copy will copy raw content from input file.
- // Optionally a different name can be given to the new file.
- func (w *Writer) Copy(name string, src *File) error {
- header := src.FileHeader
- if name != "" {
- header.Name = name
- }
- raw, err := src.OpenRaw()
- if err != nil {
- return err
- }
- wr, err := w.CreateHeaderRaw(&header)
- if err != nil {
- return err
- }
- _, err = io.Copy(wr, raw)
- return err
- }
- // detectUTF8 reports whether s is a valid UTF-8 string, and whether the string
- // must be considered UTF-8 encoding (i.e., not compatible with CP-437, ASCII,
- // or any other common encoding).
- func detectUTF8(s string) (valid, require bool) {
- for i := 0; i < len(s); {
- r, size := utf8.DecodeRuneInString(s[i:])
- i += size
- // Officially, ZIP uses CP-437, but many readers use the system's
- // local character encoding. Most encoding are compatible with a large
- // subset of CP-437, which itself is ASCII-like.
- //
- // Forbid 0x7e and 0x5c since EUC-KR and Shift-JIS replace those
- // characters with localized currency and overline characters.
- if r < 0x20 || r > 0x7d || r == 0x5c {
- if !utf8.ValidRune(r) || (r == utf8.RuneError && size == 1) {
- return false, false
- }
- require = true
- }
- }
- return true, require
- }
- // CreateHeader adds a file to the zip archive using the provided FileHeader
- // for the file metadata. Writer takes ownership of fh and may mutate
- // its fields. The caller must not modify fh after calling CreateHeader.
- //
- // This returns a Writer to which the file contents should be written.
- // The file's contents must be written to the io.Writer before the next
- // call to Create, Copy, CreateHeader, CreateHeaderRaw or Close.
- func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
- if w.last != nil && !w.last.Closed() {
- if err := w.last.Close(); err != nil {
- return nil, err
- }
- }
- if len(w.dir) > 0 && w.dir[len(w.dir)-1].FileHeader == fh {
- // See https://golang.org/issue/11144 confusion.
- return nil, errors.New("archive/zip: invalid duplicate FileHeader")
- }
- // The ZIP format has a sad state of affairs regarding character encoding.
- // Officially, the name and comment fields are supposed to be encoded
- // in CP-437 (which is mostly compatible with ASCII), unless the UTF-8
- // flag bit is set. However, there are several problems:
- //
- // * Many ZIP readers still do not support UTF-8.
- // * If the UTF-8 flag is cleared, several readers simply interpret the
- // name and comment fields as whatever the local system encoding is.
- //
- // In order to avoid breaking readers without UTF-8 support,
- // we avoid setting the UTF-8 flag if the strings are CP-437 compatible.
- // However, if the strings require multibyte UTF-8 encoding and is a
- // valid UTF-8 string, then we set the UTF-8 bit.
- //
- // For the case, where the user explicitly wants to specify the encoding
- // as UTF-8, they will need to set the flag bit themselves.
- utf8Valid1, utf8Require1 := detectUTF8(fh.Name)
- utf8Valid2, utf8Require2 := detectUTF8(fh.Comment)
- switch {
- case fh.NonUTF8:
- fh.Flags &^= 0x800
- case (utf8Require1 || utf8Require2) && (utf8Valid1 && utf8Valid2):
- fh.Flags |= 0x800
- }
- fh.CreatorVersion = fh.CreatorVersion&0xff00 | zipVersion20 // preserve compatibility byte
- fh.ReaderVersion = zipVersion20
- // If Modified is set, this takes precedence over MS-DOS timestamp fields.
- if !fh.Modified.IsZero() {
- // Contrary to the FileHeader.SetModTime method, we intentionally
- // do not convert to UTC, because we assume the user intends to encode
- // the date using the specified timezone. A user may want this control
- // because many legacy ZIP readers interpret the timestamp according
- // to the local timezone.
- //
- // The timezone is only non-UTC if a user directly sets the Modified
- // field directly themselves. All other approaches sets UTC.
- fh.ModifiedDate, fh.ModifiedTime = timeToMsDosTime(fh.Modified)
- // Use "extended timestamp" format since this is what Info-ZIP uses.
- // Nearly every major ZIP implementation uses a different format,
- // but at least most seem to be able to understand the other formats.
- //
- // This format happens to be identical for both local and central header
- // if modification time is the only timestamp being encoded.
- var mbuf [9]byte // 2*SizeOf(uint16) + SizeOf(uint8) + SizeOf(uint32)
- mt := uint32(fh.Modified.Unix())
- eb := writeBuf(mbuf[:])
- eb.uint16(extTimeExtraID)
- eb.uint16(5) // Size: SizeOf(uint8) + SizeOf(uint32)
- eb.uint8(1) // Flags: ModTime
- eb.uint32(mt) // ModTime
- fh.Extra = append(fh.Extra, mbuf[:]...)
- }
- var ow io.Writer
- h := &header{
- FileHeader: fh,
- offset: uint64(w.cw.count),
- }
- if strings.HasSuffix(fh.Name, "/") {
- // Set the compression method to Store to ensure data length is truly zero,
- // which the writeHeader method always encodes for the size fields.
- // This is necessary as most compression formats have non-zero lengths
- // even when compressing an empty string.
- fh.Method = Store
- fh.Flags &^= 0x8 // we will not write a data descriptor
- // Explicitly clear sizes as they have no meaning for directories.
- fh.CompressedSize = 0
- fh.CompressedSize64 = 0
- fh.UncompressedSize = 0
- fh.UncompressedSize64 = 0
- ow = dirWriter{}
- w.last = nil
- } else {
- fh.Flags |= 0x8 // we will write a data descriptor
- fw := &fileWriter{
- zipw: w.cw,
- compCount: &countWriter{w: w.cw},
- crc32: crc32.NewIEEE(),
- }
- comp := w.compressor(fh.Method)
- if comp == nil {
- return nil, ErrAlgorithm
- }
- var err error
- fw.comp, err = comp(fw.compCount)
- if err != nil {
- return nil, err
- }
- fw.rawCount = &countWriter{w: fw.comp}
- fw.header = h
- ow = fw
- w.last = fw
- }
- w.dir = append(w.dir, h)
- if err := writeHeader(w.cw, fh); err != nil {
- return nil, err
- }
- // If we're creating a directory, fw is nil.
- return ow, nil
- }
- // CreateHeaderRaw adds a file to the zip archive using the provided FileHeader
- // for the file metadata. Writer takes ownership of fh and may mutate
- // its fields. The caller must not modify fh after calling CreateHeaderRaw.
- //
- // This returns a Writer to which the compressed file contents should be written.
- // The file's contents must be written to the io.Writer before the next
- // call to Create, Copy, CreateHeader, CreateHeaderRaw or Close.
- //
- // Using this requires knowledge of populating the FileHeader correctly.
- // Generally using the Copy() function is recommended.
- func (w *Writer) CreateHeaderRaw(fh *FileHeader) (io.Writer, error) {
- if w.last != nil && !w.last.Closed() {
- if err := w.last.Close(); err != nil {
- return nil, err
- }
- }
- if len(w.dir) > 0 && w.dir[len(w.dir)-1].FileHeader == fh {
- // See https://golang.org/issue/11144 confusion.
- return nil, errors.New("archive/zip: invalid duplicate FileHeader")
- }
- // The ZIP format has a sad state of affairs regarding character encoding.
- // Officially, the name and comment fields are supposed to be encoded
- // in CP-437 (which is mostly compatible with ASCII), unless the UTF-8
- // flag bit is set. However, there are several problems:
- //
- // * Many ZIP readers still do not support UTF-8.
- // * If the UTF-8 flag is cleared, several readers simply interpret the
- // name and comment fields as whatever the local system encoding is.
- //
- // In order to avoid breaking readers without UTF-8 support,
- // we avoid setting the UTF-8 flag if the strings are CP-437 compatible.
- // However, if the strings require multibyte UTF-8 encoding and is a
- // valid UTF-8 string, then we set the UTF-8 bit.
- //
- // For the case, where the user explicitly wants to specify the encoding
- // as UTF-8, they will need to set the flag bit themselves.
- utf8Valid1, utf8Require1 := detectUTF8(fh.Name)
- utf8Valid2, utf8Require2 := detectUTF8(fh.Comment)
- switch {
- case fh.NonUTF8:
- fh.Flags &^= 0x800
- case (utf8Require1 || utf8Require2) && (utf8Valid1 && utf8Valid2):
- fh.Flags |= 0x800
- }
- fh.CreatorVersion = fh.CreatorVersion&0xff00 | zipVersion20 // preserve compatibility byte
- fh.ReaderVersion = zipVersion20
- // If Modified is set, this takes precedence over MS-DOS timestamp fields.
- if !fh.Modified.IsZero() {
- // Contrary to the FileHeader.SetModTime method, we intentionally
- // do not convert to UTC, because we assume the user intends to encode
- // the date using the specified timezone. A user may want this control
- // because many legacy ZIP readers interpret the timestamp according
- // to the local timezone.
- //
- // The timezone is only non-UTC if a user directly sets the Modified
- // field directly themselves. All other approaches sets UTC.
- fh.ModifiedDate, fh.ModifiedTime = timeToMsDosTime(fh.Modified)
- // Use "extended timestamp" format since this is what Info-ZIP uses.
- // Nearly every major ZIP implementation uses a different format,
- // but at least most seem to be able to understand the other formats.
- //
- // This format happens to be identical for both local and central header
- // if modification time is the only timestamp being encoded.
- var mbuf [9]byte // 2*SizeOf(uint16) + SizeOf(uint8) + SizeOf(uint32)
- mt := uint32(fh.Modified.Unix())
- eb := writeBuf(mbuf[:])
- eb.uint16(extTimeExtraID)
- eb.uint16(5) // Size: SizeOf(uint8) + SizeOf(uint32)
- eb.uint8(1) // Flags: ModTime
- eb.uint32(mt) // ModTime
- fh.Extra = append(fh.Extra, mbuf[:]...)
- }
- var ow io.Writer
- h := &header{
- FileHeader: fh,
- offset: uint64(w.cw.count),
- }
- if strings.HasSuffix(fh.Name, "/") {
- // Set the compression method to Store to ensure data length is truly zero,
- // which the writeHeader method always encodes for the size fields.
- // This is necessary as most compression formats have non-zero lengths
- // even when compressing an empty string.
- fh.Method = Store
- fh.Flags &^= 0x8 // we will not write a data descriptor
- // Explicitly clear sizes as they have no meaning for directories.
- fh.CompressedSize = 0
- fh.CompressedSize64 = 0
- fh.UncompressedSize = 0
- fh.UncompressedSize64 = 0
- ow = dirWriter{}
- w.last = nil
- } else {
- fh.Flags |= 0x8 // we will write a data descriptor
- fw := &rawWriter{
- header: h,
- zipw: w.cw,
- rawCount: &countWriter{w: w.cw},
- }
- ow = fw
- w.last = fw
- }
- w.dir = append(w.dir, h)
- if err := writeHeader(w.cw, fh); err != nil {
- return nil, err
- }
- // If we're creating a directory, fw is nil.
- return ow, nil
- }
- func writeHeader(w io.Writer, h *FileHeader) error {
- const maxUint16 = 1<<16 - 1
- if len(h.Name) > maxUint16 {
- return errLongName
- }
- if len(h.Extra) > maxUint16 {
- return errLongExtra
- }
- var buf [fileHeaderLen]byte
- b := writeBuf(buf[:])
- b.uint32(uint32(fileHeaderSignature))
- b.uint16(h.ReaderVersion)
- b.uint16(h.Flags)
- b.uint16(h.Method)
- b.uint16(h.ModifiedTime)
- b.uint16(h.ModifiedDate)
- b.uint32(0) // since we are writing a data descriptor crc32,
- b.uint32(0) // compressed size,
- b.uint32(0) // and uncompressed size should be zero
- b.uint16(uint16(len(h.Name)))
- b.uint16(uint16(len(h.Extra)))
- if _, err := w.Write(buf[:]); err != nil {
- return err
- }
- if _, err := io.WriteString(w, h.Name); err != nil {
- return err
- }
- _, err := w.Write(h.Extra)
- return err
- }
- // RegisterCompressor registers or overrides a custom compressor for a specific
- // method ID. If a compressor for a given method is not found, Writer will
- // default to looking up the compressor at the package level.
- func (w *Writer) RegisterCompressor(method uint16, comp Compressor) {
- if w.compressors == nil {
- w.compressors = make(map[uint16]Compressor)
- }
- w.compressors[method] = comp
- }
- func (w *Writer) compressor(method uint16) Compressor {
- comp := w.compressors[method]
- if comp == nil {
- comp = compressor(method)
- }
- return comp
- }
- type dirWriter struct{}
- func (dirWriter) Write(b []byte) (int, error) {
- if len(b) == 0 {
- return 0, nil
- }
- return 0, errors.New("zip: write to directory")
- }
- type fileWriter struct {
- *header
- zipw io.Writer
- rawCount *countWriter
- comp io.WriteCloser
- compCount *countWriter
- crc32 hash.Hash32
- closed bool
- }
- func (w *fileWriter) Write(p []byte) (int, error) {
- if w.closed {
- return 0, errors.New("zip: write to closed file")
- }
- w.crc32.Write(p)
- return w.rawCount.Write(p)
- }
- func (w *fileWriter) Closed() bool {
- return w.closed
- }
- func (w *fileWriter) Close() error {
- if w.closed {
- return errors.New("zip: file closed twice")
- }
- w.closed = true
- if err := w.comp.Close(); err != nil {
- return err
- }
- // update FileHeader
- fh := w.header.FileHeader
- fh.CRC32 = w.crc32.Sum32()
- fh.CompressedSize64 = uint64(w.compCount.count)
- fh.UncompressedSize64 = uint64(w.rawCount.count)
- if fh.isZip64() {
- fh.CompressedSize = uint32max
- fh.UncompressedSize = uint32max
- fh.ReaderVersion = zipVersion45 // requires 4.5 - File uses ZIP64 format extensions
- } else {
- fh.CompressedSize = uint32(fh.CompressedSize64)
- fh.UncompressedSize = uint32(fh.UncompressedSize64)
- }
- // Write data descriptor. This is more complicated than one would
- // think, see e.g. comments in zipfile.c:putextended() and
- // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073588.
- // The approach here is to write 8 byte sizes if needed without
- // adding a zip64 extra in the local header (too late anyway).
- var buf []byte
- if fh.isZip64() {
- buf = make([]byte, dataDescriptor64Len)
- } else {
- buf = make([]byte, dataDescriptorLen)
- }
- b := writeBuf(buf)
- b.uint32(dataDescriptorSignature) // de-facto standard, required by OS X
- b.uint32(fh.CRC32)
- if fh.isZip64() {
- b.uint64(fh.CompressedSize64)
- b.uint64(fh.UncompressedSize64)
- } else {
- b.uint32(fh.CompressedSize)
- b.uint32(fh.UncompressedSize)
- }
- _, err := w.zipw.Write(buf)
- return err
- }
- type rawWriter struct {
- *header
- zipw io.Writer
- rawCount *countWriter
- closed bool
- }
- func (w *rawWriter) Write(p []byte) (int, error) {
- if w.closed {
- return 0, errors.New("zip: write to closed file")
- }
- return w.rawCount.Write(p)
- }
- func (w *rawWriter) Closed() bool {
- return w.closed
- }
- func (w *rawWriter) Close() error {
- if w.closed {
- return errors.New("zip: file closed twice")
- }
- w.closed = true
- fh := w.FileHeader
- fh.CompressedSize64 = uint64(w.rawCount.count)
- // Write data descriptor. This is more complicated than one would
- // think, see e.g. comments in zipfile.c:putextended() and
- // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073588.
- // The approach here is to write 8 byte sizes if needed without
- // adding a zip64 extra in the local header (too late anyway).
- var buf []byte
- if fh.isZip64() {
- buf = make([]byte, dataDescriptor64Len)
- } else {
- buf = make([]byte, dataDescriptorLen)
- }
- b := writeBuf(buf)
- b.uint32(dataDescriptorSignature) // de-facto standard, required by OS X
- b.uint32(fh.CRC32)
- if fh.isZip64() {
- b.uint64(fh.CompressedSize64)
- b.uint64(fh.UncompressedSize64)
- } else {
- b.uint32(fh.CompressedSize)
- b.uint32(fh.UncompressedSize)
- }
- _, err := w.zipw.Write(buf)
- return err
- }
- type countWriter struct {
- w io.Writer
- count int64
- }
- func (w *countWriter) Write(p []byte) (int, error) {
- n, err := w.w.Write(p)
- w.count += int64(n)
- return n, err
- }
- type nopCloser struct {
- io.Writer
- }
- func (w nopCloser) Close() error {
- return nil
- }
- type writeBuf []byte
- func (b *writeBuf) uint8(v uint8) {
- (*b)[0] = v
- *b = (*b)[1:]
- }
- func (b *writeBuf) uint16(v uint16) {
- binary.LittleEndian.PutUint16(*b, v)
- *b = (*b)[2:]
- }
- func (b *writeBuf) uint32(v uint32) {
- binary.LittleEndian.PutUint32(*b, v)
- *b = (*b)[4:]
- }
- func (b *writeBuf) uint64(v uint64) {
- binary.LittleEndian.PutUint64(*b, v)
- *b = (*b)[8:]
- }
|