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