github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/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  )
    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  // Close finishes writing the zip file by writing the central directory.
    38  // It does not (and can not) close the underlying writer.
    39  func (w *Writer) Close() error {
    40  	if w.last != nil && !w.last.closed {
    41  		if err := w.last.close(); err != nil {
    42  			return err
    43  		}
    44  		w.last = nil
    45  	}
    46  	if w.closed {
    47  		return errors.New("zip: writer closed twice")
    48  	}
    49  	w.closed = true
    50  
    51  	// write central directory
    52  	start := w.cw.count
    53  	for _, h := range w.dir {
    54  		var buf [directoryHeaderLen]byte
    55  		b := writeBuf(buf[:])
    56  		b.uint32(uint32(directoryHeaderSignature))
    57  		b.uint16(h.CreatorVersion)
    58  		b.uint16(h.ReaderVersion)
    59  		b.uint16(h.Flags)
    60  		b.uint16(h.Method)
    61  		b.uint16(h.ModifiedTime)
    62  		b.uint16(h.ModifiedDate)
    63  		b.uint32(h.CRC32)
    64  		if h.isZip64() || h.offset > uint32max {
    65  			// the file needs a zip64 header. store maxint in both
    66  			// 32 bit size fields (and offset later) to signal that the
    67  			// zip64 extra header should be used.
    68  			b.uint32(uint32max) // compressed size
    69  			b.uint32(uint32max) // uncompressed size
    70  
    71  			// append a zip64 extra block to Extra
    72  			var buf [28]byte // 2x uint16 + 3x uint64
    73  			eb := writeBuf(buf[:])
    74  			eb.uint16(zip64ExtraId)
    75  			eb.uint16(24) // size = 3x uint64
    76  			eb.uint64(h.UncompressedSize64)
    77  			eb.uint64(h.CompressedSize64)
    78  			eb.uint64(h.offset)
    79  			h.Extra = append(h.Extra, buf[:]...)
    80  		} else {
    81  			b.uint32(h.CompressedSize)
    82  			b.uint32(h.UncompressedSize)
    83  		}
    84  		b.uint16(uint16(len(h.Name)))
    85  		b.uint16(uint16(len(h.Extra)))
    86  		b.uint16(uint16(len(h.Comment)))
    87  		b = b[4:] // skip disk number start and internal file attr (2x uint16)
    88  		b.uint32(h.ExternalAttrs)
    89  		if h.offset > uint32max {
    90  			b.uint32(uint32max)
    91  		} else {
    92  			b.uint32(uint32(h.offset))
    93  		}
    94  		if _, err := w.cw.Write(buf[:]); err != nil {
    95  			return err
    96  		}
    97  		if _, err := io.WriteString(w.cw, h.Name); err != nil {
    98  			return err
    99  		}
   100  		if _, err := w.cw.Write(h.Extra); err != nil {
   101  			return err
   102  		}
   103  		if _, err := io.WriteString(w.cw, h.Comment); err != nil {
   104  			return err
   105  		}
   106  	}
   107  	end := w.cw.count
   108  
   109  	records := uint64(len(w.dir))
   110  	size := uint64(end - start)
   111  	offset := uint64(start)
   112  
   113  	if records > uint16max || size > uint32max || offset > uint32max {
   114  		var buf [directory64EndLen + directory64LocLen]byte
   115  		b := writeBuf(buf[:])
   116  
   117  		// zip64 end of central directory record
   118  		b.uint32(directory64EndSignature)
   119  		b.uint64(directory64EndLen)
   120  		b.uint16(zipVersion45) // version made by
   121  		b.uint16(zipVersion45) // version needed to extract
   122  		b.uint32(0)            // number of this disk
   123  		b.uint32(0)            // number of the disk with the start of the central directory
   124  		b.uint64(records)      // total number of entries in the central directory on this disk
   125  		b.uint64(records)      // total number of entries in the central directory
   126  		b.uint64(size)         // size of the central directory
   127  		b.uint64(offset)       // offset of start of central directory with respect to the starting disk number
   128  
   129  		// zip64 end of central directory locator
   130  		b.uint32(directory64LocSignature)
   131  		b.uint32(0)           // number of the disk with the start of the zip64 end of central directory
   132  		b.uint64(uint64(end)) // relative offset of the zip64 end of central directory record
   133  		b.uint32(1)           // total number of disks
   134  
   135  		if _, err := w.cw.Write(buf[:]); err != nil {
   136  			return err
   137  		}
   138  
   139  		// store max values in the regular end record to signal that
   140  		// that the zip64 values should be used instead
   141  		records = uint16max
   142  		size = uint32max
   143  		offset = uint32max
   144  	}
   145  
   146  	// write end record
   147  	var buf [directoryEndLen]byte
   148  	b := writeBuf(buf[:])
   149  	b.uint32(uint32(directoryEndSignature))
   150  	b = b[4:]                 // skip over disk number and first disk number (2x uint16)
   151  	b.uint16(uint16(records)) // number of entries this disk
   152  	b.uint16(uint16(records)) // number of entries total
   153  	b.uint32(uint32(size))    // size of directory
   154  	b.uint32(uint32(offset))  // start of directory
   155  	// skipped size of comment (always zero)
   156  	if _, err := w.cw.Write(buf[:]); err != nil {
   157  		return err
   158  	}
   159  
   160  	return w.cw.w.(*bufio.Writer).Flush()
   161  }
   162  
   163  // Create adds a file to the zip file using the provided name.
   164  // It returns a Writer to which the file contents should be written.
   165  // The name must be a relative path: it must not start with a drive
   166  // letter (e.g. C:) or leading slash, and only forward slashes are
   167  // allowed.
   168  // The file's contents must be written to the io.Writer before the next
   169  // call to Create, CreateHeader, or Close.
   170  func (w *Writer) Create(name string) (io.Writer, error) {
   171  	header := &FileHeader{
   172  		Name:   name,
   173  		Method: Deflate,
   174  	}
   175  	return w.CreateHeader(header)
   176  }
   177  
   178  // CreateHeader adds a file to the zip file using the provided FileHeader
   179  // for the file metadata.
   180  // It returns a Writer to which the file contents should be written.
   181  // The file's contents must be written to the io.Writer before the next
   182  // call to Create, CreateHeader, or Close.
   183  func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
   184  	if w.last != nil && !w.last.closed {
   185  		if err := w.last.close(); err != nil {
   186  			return nil, err
   187  		}
   188  	}
   189  
   190  	fh.Flags |= 0x8 // we will write a data descriptor
   191  
   192  	fh.CreatorVersion = fh.CreatorVersion&0xff00 | zipVersion20 // preserve compatibility byte
   193  	fh.ReaderVersion = zipVersion20
   194  
   195  	fw := &fileWriter{
   196  		zipw:      w.cw,
   197  		compCount: &countWriter{w: w.cw},
   198  		crc32:     crc32.NewIEEE(),
   199  	}
   200  	comp := compressor(fh.Method)
   201  	if comp == nil {
   202  		return nil, ErrAlgorithm
   203  	}
   204  	var err error
   205  	fw.comp, err = comp(fw.compCount)
   206  	if err != nil {
   207  		return nil, err
   208  	}
   209  	fw.rawCount = &countWriter{w: fw.comp}
   210  
   211  	h := &header{
   212  		FileHeader: fh,
   213  		offset:     uint64(w.cw.count),
   214  	}
   215  	w.dir = append(w.dir, h)
   216  	fw.header = h
   217  
   218  	if err := writeHeader(w.cw, fh); err != nil {
   219  		return nil, err
   220  	}
   221  
   222  	w.last = fw
   223  	return fw, nil
   224  }
   225  
   226  func writeHeader(w io.Writer, h *FileHeader) error {
   227  	var buf [fileHeaderLen]byte
   228  	b := writeBuf(buf[:])
   229  	b.uint32(uint32(fileHeaderSignature))
   230  	b.uint16(h.ReaderVersion)
   231  	b.uint16(h.Flags)
   232  	b.uint16(h.Method)
   233  	b.uint16(h.ModifiedTime)
   234  	b.uint16(h.ModifiedDate)
   235  	b.uint32(0) // since we are writing a data descriptor crc32,
   236  	b.uint32(0) // compressed size,
   237  	b.uint32(0) // and uncompressed size should be zero
   238  	b.uint16(uint16(len(h.Name)))
   239  	b.uint16(uint16(len(h.Extra)))
   240  	if _, err := w.Write(buf[:]); err != nil {
   241  		return err
   242  	}
   243  	if _, err := io.WriteString(w, h.Name); err != nil {
   244  		return err
   245  	}
   246  	_, err := w.Write(h.Extra)
   247  	return err
   248  }
   249  
   250  type fileWriter struct {
   251  	*header
   252  	zipw      io.Writer
   253  	rawCount  *countWriter
   254  	comp      io.WriteCloser
   255  	compCount *countWriter
   256  	crc32     hash.Hash32
   257  	closed    bool
   258  }
   259  
   260  func (w *fileWriter) Write(p []byte) (int, error) {
   261  	if w.closed {
   262  		return 0, errors.New("zip: write to closed file")
   263  	}
   264  	w.crc32.Write(p)
   265  	return w.rawCount.Write(p)
   266  }
   267  
   268  func (w *fileWriter) close() error {
   269  	if w.closed {
   270  		return errors.New("zip: file closed twice")
   271  	}
   272  	w.closed = true
   273  	if err := w.comp.Close(); err != nil {
   274  		return err
   275  	}
   276  
   277  	// update FileHeader
   278  	fh := w.header.FileHeader
   279  	fh.CRC32 = w.crc32.Sum32()
   280  	fh.CompressedSize64 = uint64(w.compCount.count)
   281  	fh.UncompressedSize64 = uint64(w.rawCount.count)
   282  
   283  	if fh.isZip64() {
   284  		fh.CompressedSize = uint32max
   285  		fh.UncompressedSize = uint32max
   286  		fh.ReaderVersion = zipVersion45 // requires 4.5 - File uses ZIP64 format extensions
   287  	} else {
   288  		fh.CompressedSize = uint32(fh.CompressedSize64)
   289  		fh.UncompressedSize = uint32(fh.UncompressedSize64)
   290  	}
   291  
   292  	// Write data descriptor. This is more complicated than one would
   293  	// think, see e.g. comments in zipfile.c:putextended() and
   294  	// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073588.
   295  	// The approach here is to write 8 byte sizes if needed without
   296  	// adding a zip64 extra in the local header (too late anyway).
   297  	var buf []byte
   298  	if fh.isZip64() {
   299  		buf = make([]byte, dataDescriptor64Len)
   300  	} else {
   301  		buf = make([]byte, dataDescriptorLen)
   302  	}
   303  	b := writeBuf(buf)
   304  	b.uint32(dataDescriptorSignature) // de-facto standard, required by OS X
   305  	b.uint32(fh.CRC32)
   306  	if fh.isZip64() {
   307  		b.uint64(fh.CompressedSize64)
   308  		b.uint64(fh.UncompressedSize64)
   309  	} else {
   310  		b.uint32(fh.CompressedSize)
   311  		b.uint32(fh.UncompressedSize)
   312  	}
   313  	_, err := w.zipw.Write(buf)
   314  	return err
   315  }
   316  
   317  type countWriter struct {
   318  	w     io.Writer
   319  	count int64
   320  }
   321  
   322  func (w *countWriter) Write(p []byte) (int, error) {
   323  	n, err := w.w.Write(p)
   324  	w.count += int64(n)
   325  	return n, err
   326  }
   327  
   328  type nopCloser struct {
   329  	io.Writer
   330  }
   331  
   332  func (w nopCloser) Close() error {
   333  	return nil
   334  }
   335  
   336  type writeBuf []byte
   337  
   338  func (b *writeBuf) uint16(v uint16) {
   339  	binary.LittleEndian.PutUint16(*b, v)
   340  	*b = (*b)[2:]
   341  }
   342  
   343  func (b *writeBuf) uint32(v uint32) {
   344  	binary.LittleEndian.PutUint32(*b, v)
   345  	*b = (*b)[4:]
   346  }
   347  
   348  func (b *writeBuf) uint64(v uint64) {
   349  	binary.LittleEndian.PutUint64(*b, v)
   350  	*b = (*b)[8:]
   351  }