github.com/karrick/go@v0.0.0-20170817181416-d5b0ec858b37/src/archive/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  	"unicode/utf8"
    15  )
    16  
    17  var (
    18  	errLongName  = errors.New("zip: FileHeader.Name too long")
    19  	errLongExtra = errors.New("zip: FileHeader.Extra too long")
    20  )
    21  
    22  // Writer implements a zip file writer.
    23  type Writer struct {
    24  	cw          *countWriter
    25  	dir         []*header
    26  	last        *fileWriter
    27  	closed      bool
    28  	compressors map[uint16]Compressor
    29  
    30  	// testHookCloseSizeOffset if non-nil is called with the size
    31  	// of offset of the central directory at Close.
    32  	testHookCloseSizeOffset func(size, offset uint64)
    33  }
    34  
    35  type header struct {
    36  	*FileHeader
    37  	offset uint64
    38  }
    39  
    40  // NewWriter returns a new Writer writing a zip file to w.
    41  func NewWriter(w io.Writer) *Writer {
    42  	return &Writer{cw: &countWriter{w: bufio.NewWriter(w)}}
    43  }
    44  
    45  // SetOffset sets the offset of the beginning of the zip data within the
    46  // underlying writer. It should be used when the zip data is appended to an
    47  // existing file, such as a binary executable.
    48  // It must be called before any data is written.
    49  func (w *Writer) SetOffset(n int64) {
    50  	if w.cw.count != 0 {
    51  		panic("zip: SetOffset called after data was written")
    52  	}
    53  	w.cw.count = n
    54  }
    55  
    56  // Flush flushes any buffered data to the underlying writer.
    57  // Calling Flush is not normally necessary; calling Close is sufficient.
    58  func (w *Writer) Flush() error {
    59  	return w.cw.w.(*bufio.Writer).Flush()
    60  }
    61  
    62  // Close finishes writing the zip file by writing the central directory.
    63  // It does not (and cannot) close the underlying writer.
    64  func (w *Writer) Close() error {
    65  	if w.last != nil && !w.last.closed {
    66  		if err := w.last.close(); err != nil {
    67  			return err
    68  		}
    69  		w.last = nil
    70  	}
    71  	if w.closed {
    72  		return errors.New("zip: writer closed twice")
    73  	}
    74  	w.closed = true
    75  
    76  	// write central directory
    77  	start := w.cw.count
    78  	for _, h := range w.dir {
    79  		var buf [directoryHeaderLen]byte
    80  		b := writeBuf(buf[:])
    81  		b.uint32(uint32(directoryHeaderSignature))
    82  		b.uint16(h.CreatorVersion)
    83  		b.uint16(h.ReaderVersion)
    84  		b.uint16(h.Flags)
    85  		b.uint16(h.Method)
    86  		b.uint16(h.ModifiedTime)
    87  		b.uint16(h.ModifiedDate)
    88  		b.uint32(h.CRC32)
    89  		if h.isZip64() || h.offset >= uint32max {
    90  			// the file needs a zip64 header. store maxint in both
    91  			// 32 bit size fields (and offset later) to signal that the
    92  			// zip64 extra header should be used.
    93  			b.uint32(uint32max) // compressed size
    94  			b.uint32(uint32max) // uncompressed size
    95  
    96  			// append a zip64 extra block to Extra
    97  			var buf [28]byte // 2x uint16 + 3x uint64
    98  			eb := writeBuf(buf[:])
    99  			eb.uint16(zip64ExtraId)
   100  			eb.uint16(24) // size = 3x uint64
   101  			eb.uint64(h.UncompressedSize64)
   102  			eb.uint64(h.CompressedSize64)
   103  			eb.uint64(h.offset)
   104  			h.Extra = append(h.Extra, buf[:]...)
   105  		} else {
   106  			b.uint32(h.CompressedSize)
   107  			b.uint32(h.UncompressedSize)
   108  		}
   109  
   110  		b.uint16(uint16(len(h.Name)))
   111  		b.uint16(uint16(len(h.Extra)))
   112  		b.uint16(uint16(len(h.Comment)))
   113  		b = b[4:] // skip disk number start and internal file attr (2x uint16)
   114  		b.uint32(h.ExternalAttrs)
   115  		if h.offset > uint32max {
   116  			b.uint32(uint32max)
   117  		} else {
   118  			b.uint32(uint32(h.offset))
   119  		}
   120  		if _, err := w.cw.Write(buf[:]); err != nil {
   121  			return err
   122  		}
   123  		if _, err := io.WriteString(w.cw, h.Name); err != nil {
   124  			return err
   125  		}
   126  		if _, err := w.cw.Write(h.Extra); err != nil {
   127  			return err
   128  		}
   129  		if _, err := io.WriteString(w.cw, h.Comment); err != nil {
   130  			return err
   131  		}
   132  	}
   133  	end := w.cw.count
   134  
   135  	records := uint64(len(w.dir))
   136  	size := uint64(end - start)
   137  	offset := uint64(start)
   138  
   139  	if f := w.testHookCloseSizeOffset; f != nil {
   140  		f(size, offset)
   141  	}
   142  
   143  	if records >= uint16max || size >= uint32max || offset >= uint32max {
   144  		var buf [directory64EndLen + directory64LocLen]byte
   145  		b := writeBuf(buf[:])
   146  
   147  		// zip64 end of central directory record
   148  		b.uint32(directory64EndSignature)
   149  		b.uint64(directory64EndLen - 12) // length minus signature (uint32) and length fields (uint64)
   150  		b.uint16(zipVersion45)           // version made by
   151  		b.uint16(zipVersion45)           // version needed to extract
   152  		b.uint32(0)                      // number of this disk
   153  		b.uint32(0)                      // number of the disk with the start of the central directory
   154  		b.uint64(records)                // total number of entries in the central directory on this disk
   155  		b.uint64(records)                // total number of entries in the central directory
   156  		b.uint64(size)                   // size of the central directory
   157  		b.uint64(offset)                 // offset of start of central directory with respect to the starting disk number
   158  
   159  		// zip64 end of central directory locator
   160  		b.uint32(directory64LocSignature)
   161  		b.uint32(0)           // number of the disk with the start of the zip64 end of central directory
   162  		b.uint64(uint64(end)) // relative offset of the zip64 end of central directory record
   163  		b.uint32(1)           // total number of disks
   164  
   165  		if _, err := w.cw.Write(buf[:]); err != nil {
   166  			return err
   167  		}
   168  
   169  		// store max values in the regular end record to signal that
   170  		// that the zip64 values should be used instead
   171  		records = uint16max
   172  		size = uint32max
   173  		offset = uint32max
   174  	}
   175  
   176  	// write end record
   177  	var buf [directoryEndLen]byte
   178  	b := writeBuf(buf[:])
   179  	b.uint32(uint32(directoryEndSignature))
   180  	b = b[4:]                 // skip over disk number and first disk number (2x uint16)
   181  	b.uint16(uint16(records)) // number of entries this disk
   182  	b.uint16(uint16(records)) // number of entries total
   183  	b.uint32(uint32(size))    // size of directory
   184  	b.uint32(uint32(offset))  // start of directory
   185  	// skipped size of comment (always zero)
   186  	if _, err := w.cw.Write(buf[:]); err != nil {
   187  		return err
   188  	}
   189  
   190  	return w.cw.w.(*bufio.Writer).Flush()
   191  }
   192  
   193  // Create adds a file to the zip file using the provided name.
   194  // It returns a Writer to which the file contents should be written.
   195  // The name must be a relative path: it must not start with a drive
   196  // letter (e.g. C:) or leading slash, and only forward slashes are
   197  // allowed.
   198  // The file's contents must be written to the io.Writer before the next
   199  // call to Create, CreateHeader, or Close.
   200  func (w *Writer) Create(name string) (io.Writer, error) {
   201  	header := &FileHeader{
   202  		Name:   name,
   203  		Method: Deflate,
   204  	}
   205  	return w.CreateHeader(header)
   206  }
   207  
   208  func hasValidUTF8(s string) bool {
   209  	n := 0
   210  	for _, r := range s {
   211  		// By default, ZIP uses CP437, which is only identical to ASCII for the printable characters.
   212  		if r < 0x20 || r >= 0x7f {
   213  			if !utf8.ValidRune(r) {
   214  				return false
   215  			}
   216  			n++
   217  		}
   218  	}
   219  	return n > 0
   220  }
   221  
   222  // CreateHeader adds a file to the zip file using the provided FileHeader
   223  // for the file metadata.
   224  // It returns a Writer to which the file contents should be written.
   225  //
   226  // The file's contents must be written to the io.Writer before the next
   227  // call to Create, CreateHeader, or Close. The provided FileHeader fh
   228  // must not be modified after a call to CreateHeader.
   229  func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
   230  	if w.last != nil && !w.last.closed {
   231  		if err := w.last.close(); err != nil {
   232  			return nil, err
   233  		}
   234  	}
   235  	if len(w.dir) > 0 && w.dir[len(w.dir)-1].FileHeader == fh {
   236  		// See https://golang.org/issue/11144 confusion.
   237  		return nil, errors.New("archive/zip: invalid duplicate FileHeader")
   238  	}
   239  
   240  	fh.Flags |= 0x8 // we will write a data descriptor
   241  
   242  	if hasValidUTF8(fh.Name) || hasValidUTF8(fh.Comment) {
   243  		fh.Flags |= 0x800 // filename or comment have valid utf-8 string
   244  	}
   245  
   246  	fh.CreatorVersion = fh.CreatorVersion&0xff00 | zipVersion20 // preserve compatibility byte
   247  	fh.ReaderVersion = zipVersion20
   248  
   249  	fw := &fileWriter{
   250  		zipw:      w.cw,
   251  		compCount: &countWriter{w: w.cw},
   252  		crc32:     crc32.NewIEEE(),
   253  	}
   254  	comp := w.compressor(fh.Method)
   255  	if comp == nil {
   256  		return nil, ErrAlgorithm
   257  	}
   258  	var err error
   259  	fw.comp, err = comp(fw.compCount)
   260  	if err != nil {
   261  		return nil, err
   262  	}
   263  	fw.rawCount = &countWriter{w: fw.comp}
   264  
   265  	h := &header{
   266  		FileHeader: fh,
   267  		offset:     uint64(w.cw.count),
   268  	}
   269  	w.dir = append(w.dir, h)
   270  	fw.header = h
   271  
   272  	if err := writeHeader(w.cw, fh); err != nil {
   273  		return nil, err
   274  	}
   275  
   276  	w.last = fw
   277  	return fw, nil
   278  }
   279  
   280  func writeHeader(w io.Writer, h *FileHeader) error {
   281  	const maxUint16 = 1<<16 - 1
   282  	if len(h.Name) > maxUint16 {
   283  		return errLongName
   284  	}
   285  	if len(h.Extra) > maxUint16 {
   286  		return errLongExtra
   287  	}
   288  
   289  	var buf [fileHeaderLen]byte
   290  	b := writeBuf(buf[:])
   291  	b.uint32(uint32(fileHeaderSignature))
   292  	b.uint16(h.ReaderVersion)
   293  	b.uint16(h.Flags)
   294  	b.uint16(h.Method)
   295  	b.uint16(h.ModifiedTime)
   296  	b.uint16(h.ModifiedDate)
   297  	b.uint32(0) // since we are writing a data descriptor crc32,
   298  	b.uint32(0) // compressed size,
   299  	b.uint32(0) // and uncompressed size should be zero
   300  	b.uint16(uint16(len(h.Name)))
   301  	b.uint16(uint16(len(h.Extra)))
   302  	if _, err := w.Write(buf[:]); err != nil {
   303  		return err
   304  	}
   305  	if _, err := io.WriteString(w, h.Name); err != nil {
   306  		return err
   307  	}
   308  	_, err := w.Write(h.Extra)
   309  	return err
   310  }
   311  
   312  // RegisterCompressor registers or overrides a custom compressor for a specific
   313  // method ID. If a compressor for a given method is not found, Writer will
   314  // default to looking up the compressor at the package level.
   315  func (w *Writer) RegisterCompressor(method uint16, comp Compressor) {
   316  	if w.compressors == nil {
   317  		w.compressors = make(map[uint16]Compressor)
   318  	}
   319  	w.compressors[method] = comp
   320  }
   321  
   322  func (w *Writer) compressor(method uint16) Compressor {
   323  	comp := w.compressors[method]
   324  	if comp == nil {
   325  		comp = compressor(method)
   326  	}
   327  	return comp
   328  }
   329  
   330  type fileWriter struct {
   331  	*header
   332  	zipw      io.Writer
   333  	rawCount  *countWriter
   334  	comp      io.WriteCloser
   335  	compCount *countWriter
   336  	crc32     hash.Hash32
   337  	closed    bool
   338  }
   339  
   340  func (w *fileWriter) Write(p []byte) (int, error) {
   341  	if w.closed {
   342  		return 0, errors.New("zip: write to closed file")
   343  	}
   344  	w.crc32.Write(p)
   345  	return w.rawCount.Write(p)
   346  }
   347  
   348  func (w *fileWriter) close() error {
   349  	if w.closed {
   350  		return errors.New("zip: file closed twice")
   351  	}
   352  	w.closed = true
   353  	if err := w.comp.Close(); err != nil {
   354  		return err
   355  	}
   356  
   357  	// update FileHeader
   358  	fh := w.header.FileHeader
   359  	fh.CRC32 = w.crc32.Sum32()
   360  	fh.CompressedSize64 = uint64(w.compCount.count)
   361  	fh.UncompressedSize64 = uint64(w.rawCount.count)
   362  
   363  	if fh.isZip64() {
   364  		fh.CompressedSize = uint32max
   365  		fh.UncompressedSize = uint32max
   366  		fh.ReaderVersion = zipVersion45 // requires 4.5 - File uses ZIP64 format extensions
   367  	} else {
   368  		fh.CompressedSize = uint32(fh.CompressedSize64)
   369  		fh.UncompressedSize = uint32(fh.UncompressedSize64)
   370  	}
   371  
   372  	// Write data descriptor. This is more complicated than one would
   373  	// think, see e.g. comments in zipfile.c:putextended() and
   374  	// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073588.
   375  	// The approach here is to write 8 byte sizes if needed without
   376  	// adding a zip64 extra in the local header (too late anyway).
   377  	var buf []byte
   378  	if fh.isZip64() {
   379  		buf = make([]byte, dataDescriptor64Len)
   380  	} else {
   381  		buf = make([]byte, dataDescriptorLen)
   382  	}
   383  	b := writeBuf(buf)
   384  	b.uint32(dataDescriptorSignature) // de-facto standard, required by OS X
   385  	b.uint32(fh.CRC32)
   386  	if fh.isZip64() {
   387  		b.uint64(fh.CompressedSize64)
   388  		b.uint64(fh.UncompressedSize64)
   389  	} else {
   390  		b.uint32(fh.CompressedSize)
   391  		b.uint32(fh.UncompressedSize)
   392  	}
   393  	_, err := w.zipw.Write(buf)
   394  	return err
   395  }
   396  
   397  type countWriter struct {
   398  	w     io.Writer
   399  	count int64
   400  }
   401  
   402  func (w *countWriter) Write(p []byte) (int, error) {
   403  	n, err := w.w.Write(p)
   404  	w.count += int64(n)
   405  	return n, err
   406  }
   407  
   408  type nopCloser struct {
   409  	io.Writer
   410  }
   411  
   412  func (w nopCloser) Close() error {
   413  	return nil
   414  }
   415  
   416  type writeBuf []byte
   417  
   418  func (b *writeBuf) uint16(v uint16) {
   419  	binary.LittleEndian.PutUint16(*b, v)
   420  	*b = (*b)[2:]
   421  }
   422  
   423  func (b *writeBuf) uint32(v uint32) {
   424  	binary.LittleEndian.PutUint32(*b, v)
   425  	*b = (*b)[4:]
   426  }
   427  
   428  func (b *writeBuf) uint64(v uint64) {
   429  	binary.LittleEndian.PutUint64(*b, v)
   430  	*b = (*b)[8:]
   431  }