blob: 8a957e16367cb55a35daae13d061a04bab2dd95a [file] [log] [blame]
Dan Willemsen25a4e072016-08-05 16:34:03 -07001// 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
Colin Cross06eea2c2021-05-07 15:59:32 -0700158 // 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 }
Rex Hoffmane0c45ac2022-11-30 23:20:51 +0000165 // Only store uint32max for the size and the offset if they don't fit.
166 // Robolectric currently doesn't support zip64 and fails to find the
167 // offset to the central directory when the number of files in the zip
168 // is larger than 2^16.
169 if size > uint32max {
170 size = uint32max
171 }
172 if offset > uint32max {
173 offset = uint32max
174 }
Colin Cross06eea2c2021-05-07 15:59:32 -0700175 // END ANDROID CHANGE
Dan Willemsen25a4e072016-08-05 16:34:03 -0700176 }
177
178 // write end record
179 var buf [directoryEndLen]byte
180 b := writeBuf(buf[:])
181 b.uint32(uint32(directoryEndSignature))
182 b = b[4:] // skip over disk number and first disk number (2x uint16)
183 b.uint16(uint16(records)) // number of entries this disk
184 b.uint16(uint16(records)) // number of entries total
185 b.uint32(uint32(size)) // size of directory
186 b.uint32(uint32(offset)) // start of directory
187 // skipped size of comment (always zero)
188 if _, err := w.cw.Write(buf[:]); err != nil {
189 return err
190 }
191
192 return w.cw.w.(*bufio.Writer).Flush()
193}
194
195// Create adds a file to the zip file using the provided name.
196// It returns a Writer to which the file contents should be written.
197// The name must be a relative path: it must not start with a drive
198// letter (e.g. C:) or leading slash, and only forward slashes are
199// allowed.
200// The file's contents must be written to the io.Writer before the next
201// call to Create, CreateHeader, or Close.
202func (w *Writer) Create(name string) (io.Writer, error) {
203 header := &FileHeader{
204 Name: name,
205 Method: Deflate,
206 }
207 return w.CreateHeader(header)
208}
209
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700210// BEGIN ANDROID CHANGE separate createHeaderImpl from CreateHeader
211// Legacy version of CreateHeader
212func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
213 fh.Flags |= DataDescriptorFlag // writing a data descriptor
214 return w.createHeaderImpl(fh)
215}
216
217// END ANDROID CHANGE
218
Dan Willemsen25a4e072016-08-05 16:34:03 -0700219// 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.
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700226
227// BEGIN ANDROID CHANGE separate createHeaderImpl from CreateHeader
228func (w *Writer) createHeaderImpl(fh *FileHeader) (io.Writer, error) {
229 // END ANDROID CHANGE
Dan Willemsen25a4e072016-08-05 16:34:03 -0700230 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 }
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700239 // BEGIN ANDROID CHANGE move the setting of DataDescriptorFlag into CreateHeader
240 // fh.Flags |= 0x8 // we will write a data descriptor
241 // END ANDROID CHANGE
Dan Willemsen25a4e072016-08-05 16:34:03 -0700242 fh.CreatorVersion = fh.CreatorVersion&0xff00 | zipVersion20 // preserve compatibility byte
243 fh.ReaderVersion = zipVersion20
244
245 fw := &fileWriter{
246 zipw: w.cw,
247 compCount: &countWriter{w: w.cw},
248 crc32: crc32.NewIEEE(),
249 }
250 comp := w.compressor(fh.Method)
251 if comp == nil {
252 return nil, ErrAlgorithm
253 }
254 var err error
255 fw.comp, err = comp(fw.compCount)
256 if err != nil {
257 return nil, err
258 }
259 fw.rawCount = &countWriter{w: fw.comp}
260
261 h := &header{
262 FileHeader: fh,
263 offset: uint64(w.cw.count),
264 }
265 w.dir = append(w.dir, h)
266 fw.header = h
267
268 if err := writeHeader(w.cw, fh); err != nil {
269 return nil, err
270 }
271
272 w.last = fw
273 return fw, nil
274}
275
276func writeHeader(w io.Writer, h *FileHeader) error {
277 var buf [fileHeaderLen]byte
278 b := writeBuf(buf[:])
279 b.uint32(uint32(fileHeaderSignature))
280 b.uint16(h.ReaderVersion)
281 b.uint16(h.Flags)
282 b.uint16(h.Method)
283 b.uint16(h.ModifiedTime)
284 b.uint16(h.ModifiedDate)
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700285 // BEGIN ANDROID CHANGE populate header size fields and crc field if not writing a data descriptor
286 if h.Flags&DataDescriptorFlag != 0 {
287 // since we are writing a data descriptor, these fields should be 0
288 b.uint32(0) // crc32,
289 b.uint32(0) // compressed size,
290 b.uint32(0) // uncompressed size
291 } else {
292 b.uint32(h.CRC32)
293
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700294 compressedSize := uint32(h.CompressedSize64)
295 if compressedSize == 0 {
296 compressedSize = h.CompressedSize
297 }
298
299 uncompressedSize := uint32(h.UncompressedSize64)
300 if uncompressedSize == 0 {
301 uncompressedSize = h.UncompressedSize
302 }
303
Colin Cross373147b2020-12-17 15:08:01 -0800304 if h.CompressedSize64 > uint32max || h.UncompressedSize64 > uint32max {
305 // Sizes don't fit in a 32-bit field, put them in a zip64 extra instead.
306 compressedSize = uint32max
307 uncompressedSize = uint32max
308
309 // append a zip64 extra block to Extra
310 var buf [20]byte // 2x uint16 + 2x uint64
311 eb := writeBuf(buf[:])
312 eb.uint16(zip64ExtraId)
313 eb.uint16(16) // size = 2x uint64
314 eb.uint64(h.UncompressedSize64)
315 eb.uint64(h.CompressedSize64)
316 h.Extra = append(h.Extra, buf[:]...)
317 }
318
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700319 b.uint32(compressedSize)
320 b.uint32(uncompressedSize)
321 }
322 // END ANDROID CHANGE
Dan Willemsen25a4e072016-08-05 16:34:03 -0700323 b.uint16(uint16(len(h.Name)))
324 b.uint16(uint16(len(h.Extra)))
325 if _, err := w.Write(buf[:]); err != nil {
326 return err
327 }
328 if _, err := io.WriteString(w, h.Name); err != nil {
329 return err
330 }
331 _, err := w.Write(h.Extra)
332 return err
333}
334
335// RegisterCompressor registers or overrides a custom compressor for a specific
336// method ID. If a compressor for a given method is not found, Writer will
337// default to looking up the compressor at the package level.
338func (w *Writer) RegisterCompressor(method uint16, comp Compressor) {
339 if w.compressors == nil {
340 w.compressors = make(map[uint16]Compressor)
341 }
342 w.compressors[method] = comp
343}
344
345func (w *Writer) compressor(method uint16) Compressor {
346 comp := w.compressors[method]
347 if comp == nil {
348 comp = compressor(method)
349 }
350 return comp
351}
352
353type fileWriter struct {
354 *header
355 zipw io.Writer
356 rawCount *countWriter
357 comp io.WriteCloser
358 compCount *countWriter
359 crc32 hash.Hash32
360 closed bool
361}
362
363func (w *fileWriter) Write(p []byte) (int, error) {
364 if w.closed {
365 return 0, errors.New("zip: write to closed file")
366 }
367 w.crc32.Write(p)
368 return w.rawCount.Write(p)
369}
370
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700371// BEGIN ANDROID CHANGE give the return value a name
372func (w *fileWriter) close() (err error) {
373 // END ANDROID CHANGE
Dan Willemsen25a4e072016-08-05 16:34:03 -0700374 if w.closed {
375 return errors.New("zip: file closed twice")
376 }
377 w.closed = true
378 if err := w.comp.Close(); err != nil {
379 return err
380 }
381
382 // update FileHeader
383 fh := w.header.FileHeader
384 fh.CRC32 = w.crc32.Sum32()
385 fh.CompressedSize64 = uint64(w.compCount.count)
386 fh.UncompressedSize64 = uint64(w.rawCount.count)
387
388 if fh.isZip64() {
389 fh.CompressedSize = uint32max
390 fh.UncompressedSize = uint32max
391 fh.ReaderVersion = zipVersion45 // requires 4.5 - File uses ZIP64 format extensions
392 } else {
393 fh.CompressedSize = uint32(fh.CompressedSize64)
394 fh.UncompressedSize = uint32(fh.UncompressedSize64)
395 }
396
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700397 // BEGIN ANDROID CHANGE only write data descriptor if the flag is set
398 if fh.Flags&DataDescriptorFlag != 0 {
399 // Write data descriptor. This is more complicated than one would
400 // think, see e.g. comments in zipfile.c:putextended() and
401 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073588.
402 // The approach here is to write 8 byte sizes if needed without
403 // adding a zip64 extra in the local header (too late anyway).
404 var buf []byte
405 if fh.isZip64() {
406 buf = make([]byte, dataDescriptor64Len)
407 } else {
408 buf = make([]byte, dataDescriptorLen)
409 }
410 b := writeBuf(buf)
411 b.uint32(dataDescriptorSignature) // de-facto standard, required by OS X
412 b.uint32(fh.CRC32)
413 if fh.isZip64() {
414 b.uint64(fh.CompressedSize64)
415 b.uint64(fh.UncompressedSize64)
416 } else {
417 b.uint32(fh.CompressedSize)
418 b.uint32(fh.UncompressedSize)
419 }
420 _, err = w.zipw.Write(buf)
Dan Willemsen25a4e072016-08-05 16:34:03 -0700421 }
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700422 // END ANDROID CHANGE
Dan Willemsen25a4e072016-08-05 16:34:03 -0700423 return err
424}
425
426type countWriter struct {
427 w io.Writer
428 count int64
429}
430
431func (w *countWriter) Write(p []byte) (int, error) {
432 n, err := w.w.Write(p)
433 w.count += int64(n)
434 return n, err
435}
436
437type nopCloser struct {
438 io.Writer
439}
440
441func (w nopCloser) Close() error {
442 return nil
443}
444
445type writeBuf []byte
446
447func (b *writeBuf) uint16(v uint16) {
448 binary.LittleEndian.PutUint16(*b, v)
449 *b = (*b)[2:]
450}
451
452func (b *writeBuf) uint32(v uint32) {
453 binary.LittleEndian.PutUint32(*b, v)
454 *b = (*b)[4:]
455}
456
457func (b *writeBuf) uint64(v uint64) {
458 binary.LittleEndian.PutUint64(*b, v)
459 *b = (*b)[8:]
460}