github.phpd.cn/thought-machine/please@v12.2.0+incompatible/third_party/go/zip/writer.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package zip
     6  
     7  import (
     8  	"bufio"
     9  	"encoding/binary"
    10  	"errors"
    11  	"hash"
    12  	"hash/crc32"
    13  	"io"
    14  )
    15  
    16  // TODO(adg): support zip file comments
    17  // TODO(adg): support specifying deflate level
    18  
    19  // Writer implements a zip file writer.
    20  type Writer struct {
    21  	cw     *countWriter
    22  	dir    []*header
    23  	last   *fileWriter
    24  	closed bool
    25  }
    26  
    27  type header struct {
    28  	*FileHeader
    29  	offset uint64
    30  }
    31  
    32  // NewWriter returns a new Writer writing a zip file to w.
    33  func NewWriter(w io.Writer) *Writer {
    34  	return &Writer{cw: &countWriter{w: bufio.NewWriter(w)}}
    35  }
    36  
    37  // Flush flushes any buffered data to the underlying writer.
    38  // Calling Flush is not normally necessary; calling Close is sufficient.
    39  func (w *Writer) Flush() error {
    40  	return w.cw.w.(*bufio.Writer).Flush()
    41  }
    42  
    43  // Close finishes writing the zip file by writing the central directory.
    44  // It does not (and can not) close the underlying writer.
    45  func (w *Writer) Close() error {
    46  	if w.last != nil && !w.last.closed {
    47  		if err := w.last.close(); err != nil {
    48  			return err
    49  		}
    50  		w.last = nil
    51  	}
    52  	if w.closed {
    53  		return errors.New("zip: writer closed twice")
    54  	}
    55  	w.closed = true
    56  
    57  	// write central directory
    58  	start := w.cw.count
    59  	for _, h := range w.dir {
    60  		var buf [directoryHeaderLen]byte
    61  		b := writeBuf(buf[:])
    62  		b.uint32(uint32(directoryHeaderSignature))
    63  		b.uint16(h.CreatorVersion)
    64  		b.uint16(h.ReaderVersion)
    65  		b.uint16(h.Flags)
    66  		b.uint16(h.Method)
    67  		b.uint16(h.ModifiedTime)
    68  		b.uint16(h.ModifiedDate)
    69  		b.uint32(h.CRC32)
    70  		if h.isZip64() || h.offset > uint32max {
    71  			// the file needs a zip64 header. store maxint in both
    72  			// 32 bit size fields (and offset later) to signal that the
    73  			// zip64 extra header should be used.
    74  			b.uint32(uint32max) // compressed size
    75  			b.uint32(uint32max) // uncompressed size
    76  
    77  			// append a zip64 extra block to Extra
    78  			var buf [28]byte // 2x uint16 + 3x uint64
    79  			eb := writeBuf(buf[:])
    80  			eb.uint16(zip64ExtraId)
    81  			eb.uint16(24) // size = 3x uint64
    82  			eb.uint64(h.UncompressedSize64)
    83  			eb.uint64(h.CompressedSize64)
    84  			eb.uint64(h.offset)
    85  			h.Extra = append(h.Extra, buf[:]...)
    86  		} else {
    87  			b.uint32(h.CompressedSize)
    88  			b.uint32(h.UncompressedSize)
    89  		}
    90  		b.uint16(uint16(len(h.Name)))
    91  		b.uint16(uint16(len(h.Extra)))
    92  		b.uint16(uint16(len(h.Comment)))
    93  		b = b[4:] // skip disk number start and internal file attr (2x uint16)
    94  		b.uint32(h.ExternalAttrs)
    95  		if h.offset > uint32max {
    96  			b.uint32(uint32max)
    97  		} else {
    98  			b.uint32(uint32(h.offset))
    99  		}
   100  		if _, err := w.cw.Write(buf[:]); err != nil {
   101  			return err
   102  		}
   103  		if _, err := io.WriteString(w.cw, h.Name); err != nil {
   104  			return err
   105  		}
   106  		if _, err := w.cw.Write(h.Extra); err != nil {
   107  			return err
   108  		}
   109  		if _, err := io.WriteString(w.cw, h.Comment); err != nil {
   110  			return err
   111  		}
   112  	}
   113  	end := w.cw.count
   114  
   115  	records := uint64(len(w.dir))
   116  	size := uint64(end - start)
   117  	offset := uint64(start)
   118  
   119  	if records > uint16max || size > uint32max || offset > uint32max {
   120  		var buf [directory64EndLen + directory64LocLen]byte
   121  		b := writeBuf(buf[:])
   122  
   123  		// zip64 end of central directory record
   124  		b.uint32(directory64EndSignature)
   125  		b.uint64(directory64EndLen)
   126  		b.uint16(zipVersion45) // version made by
   127  		b.uint16(zipVersion45) // version needed to extract
   128  		b.uint32(0)            // number of this disk
   129  		b.uint32(0)            // number of the disk with the start of the central directory
   130  		b.uint64(records)      // total number of entries in the central directory on this disk
   131  		b.uint64(records)      // total number of entries in the central directory
   132  		b.uint64(size)         // size of the central directory
   133  		b.uint64(offset)       // offset of start of central directory with respect to the starting disk number
   134  
   135  		// zip64 end of central directory locator
   136  		b.uint32(directory64LocSignature)
   137  		b.uint32(0)           // number of the disk with the start of the zip64 end of central directory
   138  		b.uint64(uint64(end)) // relative offset of the zip64 end of central directory record
   139  		b.uint32(1)           // total number of disks
   140  
   141  		if _, err := w.cw.Write(buf[:]); err != nil {
   142  			return err
   143  		}
   144  
   145  		// store max values in the regular end record to signal that
   146  		// that the zip64 values should be used instead
   147  		records = uint16max
   148  		size = uint32max
   149  		offset = uint32max
   150  	}
   151  
   152  	// write end record
   153  	var buf [directoryEndLen]byte
   154  	b := writeBuf(buf[:])
   155  	b.uint32(uint32(directoryEndSignature))
   156  	b = b[4:]                 // skip over disk number and first disk number (2x uint16)
   157  	b.uint16(uint16(records)) // number of entries this disk
   158  	b.uint16(uint16(records)) // number of entries total
   159  	b.uint32(uint32(size))    // size of directory
   160  	b.uint32(uint32(offset))  // start of directory
   161  	// skipped size of comment (always zero)
   162  	if _, err := w.cw.Write(buf[:]); err != nil {
   163  		return err
   164  	}
   165  
   166  	return w.cw.w.(*bufio.Writer).Flush()
   167  }
   168  
   169  // WriteRaw writes some data to the file, not part of any of the internal structure.
   170  // This is useful for preambles; since zip files have their index at the end and
   171  // file locations are specified as offsets it's possible to lead off with something, for
   172  // example a shebang to make them self-executing as with pexes.
   173  func (w *Writer) WriteRaw(b []byte) error {
   174  	_, err := w.cw.Write(b)
   175  	return err
   176  }
   177  
   178  // Offset returns the offset that the next file would begin at.
   179  func (w *Writer) Offset() int {
   180  	return int(w.cw.count)
   181  }
   182  
   183  // Create adds a file to the zip file using the provided name.
   184  // It returns a Writer to which the file contents should be written.
   185  // The name must be a relative path: it must not start with a drive
   186  // letter (e.g. C:) or leading slash, and only forward slashes are
   187  // allowed.
   188  // The file's contents must be written to the io.Writer before the next
   189  // call to Create, CreateHeader, or Close.
   190  func (w *Writer) Create(name string) (io.Writer, error) {
   191  	header := &FileHeader{
   192  		Name:   name,
   193  		Method: Deflate,
   194  	}
   195  	return w.CreateHeader(header)
   196  }
   197  
   198  // CreateHeader adds a file to the zip file using the provided FileHeader
   199  // for the file metadata.
   200  // It returns a Writer to which the file contents should be written.
   201  // The file's contents must be written to the io.Writer before the next
   202  // call to Create, CreateHeader, or Close.
   203  func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
   204  	comp := compressor(fh.Method)
   205  	if comp == nil {
   206  		return nil, ErrAlgorithm
   207  	}
   208  	fh.Flags |= dataDescriptorFlag // we will write a data descriptor
   209  	return w.CreateHeaderWithCompressor(fh, comp, crc32.NewIEEE())
   210  }
   211  
   212  // CreateHeaderWithCompressor behaves the same as CreateHeader but allows explicitly
   213  // specifying the compressor to use, which may not necessarily correspond to the
   214  // Method property of the actual file header. Some care should therefore be taken
   215  // to ensure the zipfile will be decompressible if calling this directly.
   216  func (w *Writer) CreateHeaderWithCompressor(fh *FileHeader, comp Compressor, crc hash.Hash32) (io.Writer, error) {
   217  	if w.last != nil && !w.last.closed {
   218  		if err := w.last.close(); err != nil {
   219  			return nil, err
   220  		}
   221  	}
   222  
   223  	fh.CreatorVersion = fh.CreatorVersion&0xff00 | zipVersion20 // preserve compatibility byte
   224  	fh.ReaderVersion = zipVersion20
   225  
   226  	fw := &fileWriter{
   227  		zipw:      w.cw,
   228  		compCount: &countWriter{w: w.cw},
   229  		crc32:     crc,
   230  	}
   231  	var err error
   232  	fw.comp, err = comp(fw.compCount)
   233  	if err != nil {
   234  		return nil, err
   235  	}
   236  	fw.rawCount = &countWriter{w: fw.comp}
   237  
   238  	h := &header{
   239  		FileHeader: fh,
   240  		offset:     uint64(w.cw.count),
   241  	}
   242  	w.dir = append(w.dir, h)
   243  	fw.header = h
   244  
   245  	if err := writeHeader(w.cw, fh); err != nil {
   246  		return nil, err
   247  	}
   248  
   249  	w.last = fw
   250  	return fw, nil
   251  }
   252  
   253  func writeHeader(w io.Writer, h *FileHeader) error {
   254  	var buf [fileHeaderLen]byte
   255  	b := writeBuf(buf[:])
   256  	b.uint32(uint32(fileHeaderSignature))
   257  	b.uint16(h.ReaderVersion)
   258  	b.uint16(h.Flags)
   259  	b.uint16(h.Method)
   260  	b.uint16(h.ModifiedTime)
   261  	b.uint16(h.ModifiedDate)
   262  	if (h.Flags & dataDescriptorFlag) != 0 {
   263  		b.uint32(0) // since we are writing a data descriptor crc32,
   264  		b.uint32(0) // compressed size,
   265  		b.uint32(0) // and uncompressed size should be zero
   266  	} else {
   267  		b.uint32(h.CRC32)
   268  		b.uint32(h.CompressedSize)
   269  		b.uint32(h.UncompressedSize)
   270  	}
   271  	b.uint16(uint16(len(h.Name)))
   272  	b.uint16(uint16(len(h.Extra)))
   273  	if _, err := w.Write(buf[:]); err != nil {
   274  		return err
   275  	}
   276  	if _, err := io.WriteString(w, h.Name); err != nil {
   277  		return err
   278  	}
   279  	_, err := w.Write(h.Extra)
   280  	return err
   281  }
   282  
   283  type fileWriter struct {
   284  	*header
   285  	zipw      io.Writer
   286  	rawCount  *countWriter
   287  	comp      io.WriteCloser
   288  	compCount *countWriter
   289  	crc32     hash.Hash32
   290  	closed    bool
   291  }
   292  
   293  func (w *fileWriter) Write(p []byte) (int, error) {
   294  	if w.closed {
   295  		return 0, errors.New("zip: write to closed file")
   296  	}
   297  	w.crc32.Write(p)
   298  	return w.rawCount.Write(p)
   299  }
   300  
   301  func (w *fileWriter) close() error {
   302  	if w.closed {
   303  		return errors.New("zip: file closed twice")
   304  	}
   305  	w.closed = true
   306  	if err := w.comp.Close(); err != nil {
   307  		return err
   308  	}
   309  
   310  	// update FileHeader
   311  	fh := w.header.FileHeader
   312  	if (fh.Flags & dataDescriptorFlag) == 0 {
   313  		return nil
   314  	}
   315  	fh.CRC32 = w.crc32.Sum32()
   316  	fh.CompressedSize64 = uint64(w.compCount.count)
   317  	fh.UncompressedSize64 = uint64(w.rawCount.count)
   318  
   319  	if fh.isZip64() {
   320  		fh.CompressedSize = uint32max
   321  		fh.UncompressedSize = uint32max
   322  		fh.ReaderVersion = zipVersion45 // requires 4.5 - File uses ZIP64 format extensions
   323  	} else {
   324  		fh.CompressedSize = uint32(fh.CompressedSize64)
   325  		fh.UncompressedSize = uint32(fh.UncompressedSize64)
   326  	}
   327  
   328  	// Write data descriptor. This is more complicated than one would
   329  	// think, see e.g. comments in zipfile.c:putextended() and
   330  	// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073588.
   331  	// The approach here is to write 8 byte sizes if needed without
   332  	// adding a zip64 extra in the local header (too late anyway).
   333  	var buf []byte
   334  	if fh.isZip64() {
   335  		buf = make([]byte, dataDescriptor64Len)
   336  	} else {
   337  		buf = make([]byte, dataDescriptorLen)
   338  	}
   339  	b := writeBuf(buf)
   340  	b.uint32(dataDescriptorSignature) // de-facto standard, required by OS X
   341  	b.uint32(fh.CRC32)
   342  	if fh.isZip64() {
   343  		b.uint64(fh.CompressedSize64)
   344  		b.uint64(fh.UncompressedSize64)
   345  	} else {
   346  		b.uint32(fh.CompressedSize)
   347  		b.uint32(fh.UncompressedSize)
   348  	}
   349  	_, err := w.zipw.Write(buf)
   350  	return err
   351  }
   352  
   353  type countWriter struct {
   354  	w     io.Writer
   355  	count int64
   356  }
   357  
   358  func (w *countWriter) Write(p []byte) (int, error) {
   359  	n, err := w.w.Write(p)
   360  	w.count += int64(n)
   361  	return n, err
   362  }
   363  
   364  type nopCloser struct {
   365  	io.Writer
   366  }
   367  
   368  func (w nopCloser) Close() error {
   369  	return nil
   370  }
   371  
   372  type writeBuf []byte
   373  
   374  func (b *writeBuf) uint16(v uint16) {
   375  	binary.LittleEndian.PutUint16(*b, v)
   376  	*b = (*b)[2:]
   377  }
   378  
   379  func (b *writeBuf) uint32(v uint32) {
   380  	binary.LittleEndian.PutUint32(*b, v)
   381  	*b = (*b)[4:]
   382  }
   383  
   384  func (b *writeBuf) uint64(v uint64) {
   385  	binary.LittleEndian.PutUint64(*b, v)
   386  	*b = (*b)[8:]
   387  }