2011-07-09 19:30:16 -06:00
|
|
|
// 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"
|
2012-02-27 15:41:30 -07:00
|
|
|
"encoding/binary"
|
2011-11-01 20:04:37 -06:00
|
|
|
"errors"
|
2011-07-09 19:30:16 -06:00
|
|
|
"hash"
|
|
|
|
"hash/crc32"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO(adg): support zip file comments
|
|
|
|
// TODO(adg): support specifying deflate level
|
|
|
|
|
|
|
|
// Writer implements a zip file writer.
|
|
|
|
type Writer struct {
|
2012-02-13 16:47:48 -07:00
|
|
|
cw *countWriter
|
2011-07-09 19:30:16 -06:00
|
|
|
dir []*header
|
|
|
|
last *fileWriter
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type header struct {
|
|
|
|
*FileHeader
|
2012-08-21 19:05:24 -06:00
|
|
|
offset uint64
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWriter returns a new Writer writing a zip file to w.
|
|
|
|
func NewWriter(w io.Writer) *Writer {
|
2012-02-13 16:47:48 -07:00
|
|
|
return &Writer{cw: &countWriter{w: bufio.NewWriter(w)}}
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close finishes writing the zip file by writing the central directory.
|
|
|
|
// It does not (and can not) close the underlying writer.
|
2012-02-26 22:29:22 -07:00
|
|
|
func (w *Writer) Close() error {
|
2011-07-09 19:30:16 -06:00
|
|
|
if w.last != nil && !w.last.closed {
|
2012-02-26 22:29:22 -07:00
|
|
|
if err := w.last.close(); err != nil {
|
|
|
|
return err
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
|
|
|
w.last = nil
|
|
|
|
}
|
|
|
|
if w.closed {
|
2011-11-01 20:04:37 -06:00
|
|
|
return errors.New("zip: writer closed twice")
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
|
|
|
w.closed = true
|
|
|
|
|
|
|
|
// write central directory
|
2012-02-13 16:47:48 -07:00
|
|
|
start := w.cw.count
|
2011-07-09 19:30:16 -06:00
|
|
|
for _, h := range w.dir {
|
2012-02-26 23:37:59 -07:00
|
|
|
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)
|
2012-08-21 19:05:24 -06:00
|
|
|
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)
|
|
|
|
}
|
2012-02-26 23:37:59 -07:00
|
|
|
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)
|
2012-08-21 19:05:24 -06:00
|
|
|
if h.offset > uint32max {
|
|
|
|
b.uint32(uint32max)
|
|
|
|
} else {
|
|
|
|
b.uint32(uint32(h.offset))
|
|
|
|
}
|
2012-02-26 23:37:59 -07:00
|
|
|
if _, err := w.cw.Write(buf[:]); err != nil {
|
2012-02-26 22:29:22 -07:00
|
|
|
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
|
|
|
|
}
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
2012-02-13 16:47:48 -07:00
|
|
|
end := w.cw.count
|
2011-07-09 19:30:16 -06:00
|
|
|
|
2012-08-21 19:05:24 -06:00
|
|
|
records := uint64(len(w.dir))
|
|
|
|
size := uint64(end - start)
|
|
|
|
offset := uint64(start)
|
|
|
|
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
// that the zip64 values should be used instead
|
|
|
|
records = uint16max
|
|
|
|
size = uint32max
|
|
|
|
offset = uint32max
|
|
|
|
}
|
|
|
|
|
2011-07-09 19:30:16 -06:00
|
|
|
// write end record
|
2012-02-26 23:37:59 -07:00
|
|
|
var buf [directoryEndLen]byte
|
|
|
|
b := writeBuf(buf[:])
|
|
|
|
b.uint32(uint32(directoryEndSignature))
|
2012-08-21 19:05:24 -06:00
|
|
|
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
|
2012-02-26 22:29:22 -07:00
|
|
|
// skipped size of comment (always zero)
|
2012-02-26 23:37:59 -07:00
|
|
|
if _, err := w.cw.Write(buf[:]); err != nil {
|
2012-02-26 22:29:22 -07:00
|
|
|
return err
|
|
|
|
}
|
2012-02-13 16:47:48 -07:00
|
|
|
|
|
|
|
return w.cw.w.(*bufio.Writer).Flush()
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create adds a file to the zip file using the provided name.
|
|
|
|
// It returns a Writer to which the file contents should be written.
|
2013-04-17 14:25:12 -06:00
|
|
|
// 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.
|
2011-07-09 19:30:16 -06:00
|
|
|
// The file's contents must be written to the io.Writer before the next
|
|
|
|
// call to Create, CreateHeader, or Close.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (w *Writer) Create(name string) (io.Writer, error) {
|
2011-07-09 19:30:16 -06:00
|
|
|
header := &FileHeader{
|
|
|
|
Name: name,
|
|
|
|
Method: Deflate,
|
|
|
|
}
|
|
|
|
return w.CreateHeader(header)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateHeader adds a file to the zip file using the provided FileHeader
|
2012-10-30 14:38:01 -06:00
|
|
|
// for the file metadata.
|
2011-07-09 19:30:16 -06:00
|
|
|
// It 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, CreateHeader, or Close.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
|
2011-07-09 19:30:16 -06:00
|
|
|
if w.last != nil && !w.last.closed {
|
|
|
|
if err := w.last.close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fh.Flags |= 0x8 // we will write a data descriptor
|
2012-08-21 19:05:24 -06:00
|
|
|
|
|
|
|
fh.CreatorVersion = fh.CreatorVersion&0xff00 | zipVersion20 // preserve compatibility byte
|
|
|
|
fh.ReaderVersion = zipVersion20
|
2011-07-09 19:30:16 -06:00
|
|
|
|
|
|
|
fw := &fileWriter{
|
2012-02-13 16:47:48 -07:00
|
|
|
zipw: w.cw,
|
|
|
|
compCount: &countWriter{w: w.cw},
|
2011-07-09 19:30:16 -06:00
|
|
|
crc32: crc32.NewIEEE(),
|
|
|
|
}
|
2013-08-06 13:03:38 -06:00
|
|
|
comp := compressor(fh.Method)
|
|
|
|
if comp == nil {
|
2012-01-24 12:48:48 -07:00
|
|
|
return nil, ErrAlgorithm
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
2013-08-06 13:03:38 -06:00
|
|
|
var err error
|
|
|
|
fw.comp, err = comp(fw.compCount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2011-07-09 19:30:16 -06:00
|
|
|
fw.rawCount = &countWriter{w: fw.comp}
|
|
|
|
|
|
|
|
h := &header{
|
|
|
|
FileHeader: fh,
|
2012-08-21 19:05:24 -06:00
|
|
|
offset: uint64(w.cw.count),
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
|
|
|
w.dir = append(w.dir, h)
|
|
|
|
fw.header = h
|
|
|
|
|
2012-02-13 16:47:48 -07:00
|
|
|
if err := writeHeader(w.cw, fh); err != nil {
|
2011-07-09 19:30:16 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.last = fw
|
|
|
|
return fw, nil
|
|
|
|
}
|
|
|
|
|
2012-02-26 22:29:22 -07:00
|
|
|
func writeHeader(w io.Writer, h *FileHeader) error {
|
2012-02-26 23:37:59 -07:00
|
|
|
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)
|
2012-08-21 19:05:24 -06:00
|
|
|
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
|
2012-02-26 23:37:59 -07:00
|
|
|
b.uint16(uint16(len(h.Name)))
|
|
|
|
b.uint16(uint16(len(h.Extra)))
|
|
|
|
if _, err := w.Write(buf[:]); err != nil {
|
2012-02-26 22:29:22 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.WriteString(w, h.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := w.Write(h.Extra)
|
|
|
|
return err
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type fileWriter struct {
|
|
|
|
*header
|
|
|
|
zipw io.Writer
|
|
|
|
rawCount *countWriter
|
|
|
|
comp io.WriteCloser
|
|
|
|
compCount *countWriter
|
|
|
|
crc32 hash.Hash32
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:04:37 -06:00
|
|
|
func (w *fileWriter) Write(p []byte) (int, error) {
|
2011-07-09 19:30:16 -06:00
|
|
|
if w.closed {
|
2011-11-01 20:04:37 -06:00
|
|
|
return 0, errors.New("zip: write to closed file")
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
|
|
|
w.crc32.Write(p)
|
|
|
|
return w.rawCount.Write(p)
|
|
|
|
}
|
|
|
|
|
2012-02-26 22:29:22 -07:00
|
|
|
func (w *fileWriter) close() error {
|
2011-07-09 19:30:16 -06:00
|
|
|
if w.closed {
|
2011-11-01 20:04:37 -06:00
|
|
|
return errors.New("zip: file closed twice")
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
|
|
|
w.closed = true
|
2012-02-26 22:29:22 -07:00
|
|
|
if err := w.comp.Close(); err != nil {
|
|
|
|
return err
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// update FileHeader
|
|
|
|
fh := w.header.FileHeader
|
|
|
|
fh.CRC32 = w.crc32.Sum32()
|
2012-08-21 19:05:24 -06:00
|
|
|
fh.CompressedSize64 = uint64(w.compCount.count)
|
|
|
|
fh.UncompressedSize64 = uint64(w.rawCount.count)
|
2011-07-09 19:30:16 -06:00
|
|
|
|
2012-08-21 19:05:24 -06:00
|
|
|
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)
|
2012-03-09 15:12:02 -07:00
|
|
|
b.uint32(dataDescriptorSignature) // de-facto standard, required by OS X
|
2012-02-26 23:37:59 -07:00
|
|
|
b.uint32(fh.CRC32)
|
2012-08-21 19:05:24 -06:00
|
|
|
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)
|
2012-02-26 22:29:22 -07:00
|
|
|
return err
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type countWriter struct {
|
|
|
|
w io.Writer
|
|
|
|
count int64
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:04:37 -06:00
|
|
|
func (w *countWriter) Write(p []byte) (int, error) {
|
2011-07-09 19:30:16 -06:00
|
|
|
n, err := w.w.Write(p)
|
|
|
|
w.count += int64(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type nopCloser struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:04:37 -06:00
|
|
|
func (w nopCloser) Close() error {
|
2011-07-09 19:30:16 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-02-26 23:37:59 -07:00
|
|
|
type writeBuf []byte
|
|
|
|
|
|
|
|
func (b *writeBuf) uint16(v uint16) {
|
2012-02-27 15:41:30 -07:00
|
|
|
binary.LittleEndian.PutUint16(*b, v)
|
2012-02-26 23:37:59 -07:00
|
|
|
*b = (*b)[2:]
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
|
|
|
|
2012-02-26 23:37:59 -07:00
|
|
|
func (b *writeBuf) uint32(v uint32) {
|
2012-02-27 15:41:30 -07:00
|
|
|
binary.LittleEndian.PutUint32(*b, v)
|
2012-02-26 23:37:59 -07:00
|
|
|
*b = (*b)[4:]
|
2011-07-09 19:30:16 -06:00
|
|
|
}
|
2012-08-21 19:05:24 -06:00
|
|
|
|
|
|
|
func (b *writeBuf) uint64(v uint64) {
|
|
|
|
binary.LittleEndian.PutUint64(*b, v)
|
|
|
|
*b = (*b)[8:]
|
|
|
|
}
|