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.
19 errLongName = errors.New("zip: FileHeader.Name too long")
20 errLongExtra = errors.New("zip: FileHeader.Extra too long")
23 // Writer implements a zip file writer.
29 compressors map[uint16]Compressor
32 // testHookCloseSizeOffset if non-nil is called with the size
33 // of offset of the central directory at Close.
34 testHookCloseSizeOffset func(size, offset uint64)
43 // NewWriter returns a new Writer writing a zip file to w.
44 func NewWriter(w io.Writer) *Writer {
45 return &Writer{cw: &countWriter{w: bufio.NewWriter(w)}}
48 // SetOffset sets the offset of the beginning of the zip data within the
49 // underlying writer. It should be used when the zip data is appended to an
50 // existing file, such as a binary executable.
51 // It must be called before any data is written.
52 func (w *Writer) SetOffset(n int64) {
54 panic("zip: SetOffset called after data was written")
59 // Flush flushes any buffered data to the underlying writer.
60 // Calling Flush is not normally necessary; calling Close is sufficient.
61 func (w *Writer) Flush() error {
62 return w.cw.w.(*bufio.Writer).Flush()
65 // SetComment sets the end-of-central-directory comment field.
66 // It can only be called before Close.
67 func (w *Writer) SetComment(comment string) error {
68 if len(comment) > uint16max {
69 return errors.New("zip: Writer.Comment too long")
75 // Close finishes writing the zip file by writing the central directory.
76 // It does not close the underlying writer.
77 func (w *Writer) Close() error {
78 if w.last != nil && !w.last.closed {
79 if err := w.last.close(); err != nil {
85 return errors.New("zip: writer closed twice")
89 // write central directory
91 for _, h := range w.dir {
92 var buf [directoryHeaderLen]byte
94 b.uint32(uint32(directoryHeaderSignature))
95 b.uint16(h.CreatorVersion)
96 b.uint16(h.ReaderVersion)
99 b.uint16(h.ModifiedTime)
100 b.uint16(h.ModifiedDate)
102 if h.isZip64() || h.offset >= uint32max {
103 // the file needs a zip64 header. store maxint in both
104 // 32 bit size fields (and offset later) to signal that the
105 // zip64 extra header should be used.
106 b.uint32(uint32max) // compressed size
107 b.uint32(uint32max) // uncompressed size
109 // append a zip64 extra block to Extra
110 var buf [28]byte // 2x uint16 + 3x uint64
111 eb := writeBuf(buf[:])
112 eb.uint16(zip64ExtraID)
113 eb.uint16(24) // size = 3x uint64
114 eb.uint64(h.UncompressedSize64)
115 eb.uint64(h.CompressedSize64)
117 h.Extra = append(h.Extra, buf[:]...)
119 b.uint32(h.CompressedSize)
120 b.uint32(h.UncompressedSize)
123 b.uint16(uint16(len(h.Name)))
124 b.uint16(uint16(len(h.Extra)))
125 b.uint16(uint16(len(h.Comment)))
126 b = b[4:] // skip disk number start and internal file attr (2x uint16)
127 b.uint32(h.ExternalAttrs)
128 if h.offset > uint32max {
131 b.uint32(uint32(h.offset))
133 if _, err := w.cw.Write(buf[:]); err != nil {
136 if _, err := io.WriteString(w.cw, h.Name); err != nil {
139 if _, err := w.cw.Write(h.Extra); err != nil {
142 if _, err := io.WriteString(w.cw, h.Comment); err != nil {
148 records := uint64(len(w.dir))
149 size := uint64(end - start)
150 offset := uint64(start)
152 if f := w.testHookCloseSizeOffset; f != nil {
156 if records >= uint16max || size >= uint32max || offset >= uint32max {
157 var buf [directory64EndLen + directory64LocLen]byte
158 b := writeBuf(buf[:])
160 // zip64 end of central directory record
161 b.uint32(directory64EndSignature)
162 b.uint64(directory64EndLen - 12) // length minus signature (uint32) and length fields (uint64)
163 b.uint16(zipVersion45) // version made by
164 b.uint16(zipVersion45) // version needed to extract
165 b.uint32(0) // number of this disk
166 b.uint32(0) // number of the disk with the start of the central directory
167 b.uint64(records) // total number of entries in the central directory on this disk
168 b.uint64(records) // total number of entries in the central directory
169 b.uint64(size) // size of the central directory
170 b.uint64(offset) // offset of start of central directory with respect to the starting disk number
172 // zip64 end of central directory locator
173 b.uint32(directory64LocSignature)
174 b.uint32(0) // number of the disk with the start of the zip64 end of central directory
175 b.uint64(uint64(end)) // relative offset of the zip64 end of central directory record
176 b.uint32(1) // total number of disks
178 if _, err := w.cw.Write(buf[:]); err != nil {
182 // store max values in the regular end record to signal
183 // that the zip64 values should be used instead
190 var buf [directoryEndLen]byte
191 b := writeBuf(buf[:])
192 b.uint32(uint32(directoryEndSignature))
193 b = b[4:] // skip over disk number and first disk number (2x uint16)
194 b.uint16(uint16(records)) // number of entries this disk
195 b.uint16(uint16(records)) // number of entries total
196 b.uint32(uint32(size)) // size of directory
197 b.uint32(uint32(offset)) // start of directory
198 b.uint16(uint16(len(w.comment))) // byte size of EOCD comment
199 if _, err := w.cw.Write(buf[:]); err != nil {
202 if _, err := io.WriteString(w.cw, w.comment); err != nil {
206 return w.cw.w.(*bufio.Writer).Flush()
209 // Create adds a file to the zip file using the provided name.
210 // It returns a Writer to which the file contents should be written.
211 // The file contents will be compressed using the Deflate method.
212 // The name must be a relative path: it must not start with a drive
213 // letter (e.g. C:) or leading slash, and only forward slashes are
214 // allowed. To create a directory instead of a file, add a trailing
215 // slash to the name.
216 // The file's contents must be written to the io.Writer before the next
217 // call to Create, CreateHeader, or Close.
218 func (w *Writer) Create(name string) (io.Writer, error) {
219 header := &FileHeader{
223 return w.CreateHeader(header)
226 // detectUTF8 reports whether s is a valid UTF-8 string, and whether the string
227 // must be considered UTF-8 encoding (i.e., not compatible with CP-437, ASCII,
228 // or any other common encoding).
229 func detectUTF8(s string) (valid, require bool) {
230 for i := 0; i < len(s); {
231 r, size := utf8.DecodeRuneInString(s[i:])
233 // Officially, ZIP uses CP-437, but many readers use the system's
234 // local character encoding. Most encoding are compatible with a large
235 // subset of CP-437, which itself is ASCII-like.
237 // Forbid 0x7e and 0x5c since EUC-KR and Shift-JIS replace those
238 // characters with localized currency and overline characters.
239 if r < 0x20 || r > 0x7d || r == 0x5c {
240 if !utf8.ValidRune(r) || (r == utf8.RuneError && size == 1) {
249 // prepare performs the bookkeeping operations required at the start of
250 // CreateHeader and CreateRaw.
251 func (w *Writer) prepare(fh *FileHeader) error {
252 if w.last != nil && !w.last.closed {
253 if err := w.last.close(); err != nil {
257 if len(w.dir) > 0 && w.dir[len(w.dir)-1].FileHeader == fh {
258 // See https://golang.org/issue/11144 confusion.
259 return errors.New("archive/zip: invalid duplicate FileHeader")
264 // CreateHeader adds a file to the zip archive using the provided FileHeader
265 // for the file metadata. Writer takes ownership of fh and may mutate
266 // its fields. The caller must not modify fh after calling CreateHeader.
268 // This returns a Writer to which the file contents should be written.
269 // The file's contents must be written to the io.Writer before the next
270 // call to Create, CreateHeader, CreateRaw, or Close.
271 func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
272 if err := w.prepare(fh); err != nil {
276 // The ZIP format has a sad state of affairs regarding character encoding.
277 // Officially, the name and comment fields are supposed to be encoded
278 // in CP-437 (which is mostly compatible with ASCII), unless the UTF-8
279 // flag bit is set. However, there are several problems:
281 // * Many ZIP readers still do not support UTF-8.
282 // * If the UTF-8 flag is cleared, several readers simply interpret the
283 // name and comment fields as whatever the local system encoding is.
285 // In order to avoid breaking readers without UTF-8 support,
286 // we avoid setting the UTF-8 flag if the strings are CP-437 compatible.
287 // However, if the strings require multibyte UTF-8 encoding and is a
288 // valid UTF-8 string, then we set the UTF-8 bit.
290 // For the case, where the user explicitly wants to specify the encoding
291 // as UTF-8, they will need to set the flag bit themselves.
292 utf8Valid1, utf8Require1 := detectUTF8(fh.Name)
293 utf8Valid2, utf8Require2 := detectUTF8(fh.Comment)
297 case (utf8Require1 || utf8Require2) && (utf8Valid1 && utf8Valid2):
301 fh.CreatorVersion = fh.CreatorVersion&0xff00 | zipVersion20 // preserve compatibility byte
302 fh.ReaderVersion = zipVersion20
304 // If Modified is set, this takes precedence over MS-DOS timestamp fields.
305 if !fh.Modified.IsZero() {
306 // Contrary to the FileHeader.SetModTime method, we intentionally
307 // do not convert to UTC, because we assume the user intends to encode
308 // the date using the specified timezone. A user may want this control
309 // because many legacy ZIP readers interpret the timestamp according
310 // to the local timezone.
312 // The timezone is only non-UTC if a user directly sets the Modified
313 // field directly themselves. All other approaches sets UTC.
314 fh.ModifiedDate, fh.ModifiedTime = timeToMsDosTime(fh.Modified)
316 // Use "extended timestamp" format since this is what Info-ZIP uses.
317 // Nearly every major ZIP implementation uses a different format,
318 // but at least most seem to be able to understand the other formats.
320 // This format happens to be identical for both local and central header
321 // if modification time is the only timestamp being encoded.
322 var mbuf [9]byte // 2*SizeOf(uint16) + SizeOf(uint8) + SizeOf(uint32)
323 mt := uint32(fh.Modified.Unix())
324 eb := writeBuf(mbuf[:])
325 eb.uint16(extTimeExtraID)
326 eb.uint16(5) // Size: SizeOf(uint8) + SizeOf(uint32)
327 eb.uint8(1) // Flags: ModTime
328 eb.uint32(mt) // ModTime
329 fh.Extra = append(fh.Extra, mbuf[:]...)
338 offset: uint64(w.cw.count),
341 if strings.HasSuffix(fh.Name, "/") {
342 // Set the compression method to Store to ensure data length is truly zero,
343 // which the writeHeader method always encodes for the size fields.
344 // This is necessary as most compression formats have non-zero lengths
345 // even when compressing an empty string.
347 fh.Flags &^= 0x8 // we will not write a data descriptor
349 // Explicitly clear sizes as they have no meaning for directories.
350 fh.CompressedSize = 0
351 fh.CompressedSize64 = 0
352 fh.UncompressedSize = 0
353 fh.UncompressedSize64 = 0
357 fh.Flags |= 0x8 // we will write a data descriptor
361 compCount: &countWriter{w: w.cw},
362 crc32: crc32.NewIEEE(),
364 comp := w.compressor(fh.Method)
366 return nil, ErrAlgorithm
369 fw.comp, err = comp(fw.compCount)
373 fw.rawCount = &countWriter{w: fw.comp}
377 w.dir = append(w.dir, h)
378 if err := writeHeader(w.cw, h); err != nil {
381 // If we're creating a directory, fw is nil.
386 func writeHeader(w io.Writer, h *header) error {
387 const maxUint16 = 1<<16 - 1
388 if len(h.Name) > maxUint16 {
391 if len(h.Extra) > maxUint16 {
395 var buf [fileHeaderLen]byte
396 b := writeBuf(buf[:])
397 b.uint32(uint32(fileHeaderSignature))
398 b.uint16(h.ReaderVersion)
401 b.uint16(h.ModifiedTime)
402 b.uint16(h.ModifiedDate)
403 // In raw mode (caller does the compression), the values are either
404 // written here or in the trailing data descriptor based on the header
406 if h.raw && !h.hasDataDescriptor() {
408 b.uint32(uint32(min64(h.CompressedSize64, uint32max)))
409 b.uint32(uint32(min64(h.UncompressedSize64, uint32max)))
411 // When this package handle the compression, these values are
412 // always written to the trailing data descriptor.
414 b.uint32(0) // compressed size
415 b.uint32(0) // uncompressed size
417 b.uint16(uint16(len(h.Name)))
418 b.uint16(uint16(len(h.Extra)))
419 if _, err := w.Write(buf[:]); err != nil {
422 if _, err := io.WriteString(w, h.Name); err != nil {
425 _, err := w.Write(h.Extra)
429 func min64(x, y uint64) uint64 {
436 // CreateRaw adds a file to the zip archive using the provided FileHeader and
437 // returns a Writer to which the file contents should be written. The file's
438 // contents must be written to the io.Writer before the next call to Create,
439 // CreateHeader, CreateRaw, or Close.
441 // In contrast to CreateHeader, the bytes passed to Writer are not compressed.
442 func (w *Writer) CreateRaw(fh *FileHeader) (io.Writer, error) {
443 if err := w.prepare(fh); err != nil {
447 fh.CompressedSize = uint32(min64(fh.CompressedSize64, uint32max))
448 fh.UncompressedSize = uint32(min64(fh.UncompressedSize64, uint32max))
452 offset: uint64(w.cw.count),
455 w.dir = append(w.dir, h)
456 if err := writeHeader(w.cw, h); err != nil {
460 if strings.HasSuffix(fh.Name, "/") {
462 return dirWriter{}, nil
473 // Copy copies the file f (obtained from a Reader) into w. It copies the raw
474 // form directly bypassing decompression, compression, and validation.
475 func (w *Writer) Copy(f *File) error {
476 r, err := f.OpenRaw()
480 fw, err := w.CreateRaw(&f.FileHeader)
484 _, err = io.Copy(fw, r)
488 // RegisterCompressor registers or overrides a custom compressor for a specific
489 // method ID. If a compressor for a given method is not found, Writer will
490 // default to looking up the compressor at the package level.
491 func (w *Writer) RegisterCompressor(method uint16, comp Compressor) {
492 if w.compressors == nil {
493 w.compressors = make(map[uint16]Compressor)
495 w.compressors[method] = comp
498 func (w *Writer) compressor(method uint16) Compressor {
499 comp := w.compressors[method]
501 comp = compressor(method)
506 type dirWriter struct{}
508 func (dirWriter) Write(b []byte) (int, error) {
512 return 0, errors.New("zip: write to directory")
515 type fileWriter struct {
518 rawCount *countWriter
520 compCount *countWriter
525 func (w *fileWriter) Write(p []byte) (int, error) {
527 return 0, errors.New("zip: write to closed file")
530 return w.zipw.Write(p)
533 return w.rawCount.Write(p)
536 func (w *fileWriter) close() error {
538 return errors.New("zip: file closed twice")
542 return w.writeDataDescriptor()
544 if err := w.comp.Close(); err != nil {
549 fh := w.header.FileHeader
550 fh.CRC32 = w.crc32.Sum32()
551 fh.CompressedSize64 = uint64(w.compCount.count)
552 fh.UncompressedSize64 = uint64(w.rawCount.count)
555 fh.CompressedSize = uint32max
556 fh.UncompressedSize = uint32max
557 fh.ReaderVersion = zipVersion45 // requires 4.5 - File uses ZIP64 format extensions
559 fh.CompressedSize = uint32(fh.CompressedSize64)
560 fh.UncompressedSize = uint32(fh.UncompressedSize64)
563 return w.writeDataDescriptor()
566 func (w *fileWriter) writeDataDescriptor() error {
567 if !w.hasDataDescriptor() {
570 // Write data descriptor. This is more complicated than one would
571 // think, see e.g. comments in zipfile.c:putextended() and
572 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073588.
573 // The approach here is to write 8 byte sizes if needed without
574 // adding a zip64 extra in the local header (too late anyway).
577 buf = make([]byte, dataDescriptor64Len)
579 buf = make([]byte, dataDescriptorLen)
582 b.uint32(dataDescriptorSignature) // de-facto standard, required by OS X
585 b.uint64(w.CompressedSize64)
586 b.uint64(w.UncompressedSize64)
588 b.uint32(w.CompressedSize)
589 b.uint32(w.UncompressedSize)
591 _, err := w.zipw.Write(buf)
595 type countWriter struct {
600 func (w *countWriter) Write(p []byte) (int, error) {
601 n, err := w.w.Write(p)
606 type nopCloser struct {
610 func (w nopCloser) Close() error {
616 func (b *writeBuf) uint8(v uint8) {
621 func (b *writeBuf) uint16(v uint16) {
622 binary.LittleEndian.PutUint16(*b, v)
626 func (b *writeBuf) uint32(v uint32) {
627 binary.LittleEndian.PutUint32(*b, v)
631 func (b *writeBuf) uint64(v uint64) {
632 binary.LittleEndian.PutUint64(*b, v)