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