1
2
3
4
5 package zip
6
7 import (
8 "bufio"
9 "encoding/binary"
10 "errors"
11 "hash"
12 "hash/crc32"
13 "io"
14 "io/fs"
15 "strings"
16 "unicode/utf8"
17 )
18
19 var (
20 errLongName = errors.New("zip: FileHeader.Name too long")
21 errLongExtra = errors.New("zip: FileHeader.Extra too long")
22 )
23
24
25 type Writer struct {
26 cw *countWriter
27 dir []*header
28 last *fileWriter
29 closed bool
30 compressors map[uint16]Compressor
31 comment string
32
33
34
35 testHookCloseSizeOffset func(size, offset uint64)
36 }
37
38 type header struct {
39 *FileHeader
40 offset uint64
41 raw bool
42 }
43
44
45 func NewWriter(w io.Writer) *Writer {
46 return &Writer{cw: &countWriter{w: bufio.NewWriter(w)}}
47 }
48
49
50
51
52
53 func (w *Writer) SetOffset(n int64) {
54 if w.cw.count != 0 {
55 panic("zip: SetOffset called after data was written")
56 }
57 w.cw.count = n
58 }
59
60
61
62 func (w *Writer) Flush() error {
63 return w.cw.w.(*bufio.Writer).Flush()
64 }
65
66
67
68 func (w *Writer) SetComment(comment string) error {
69 if len(comment) > uint16max {
70 return errors.New("zip: Writer.Comment too long")
71 }
72 w.comment = comment
73 return nil
74 }
75
76
77
78 func (w *Writer) Close() error {
79 if w.last != nil && !w.last.closed {
80 if err := w.last.close(); err != nil {
81 return err
82 }
83 w.last = nil
84 }
85 if w.closed {
86 return errors.New("zip: writer closed twice")
87 }
88 w.closed = true
89
90
91 start := w.cw.count
92 for _, h := range w.dir {
93 var buf [directoryHeaderLen]byte
94 b := writeBuf(buf[:])
95 b.uint32(uint32(directoryHeaderSignature))
96 b.uint16(h.CreatorVersion)
97 b.uint16(h.ReaderVersion)
98 b.uint16(h.Flags)
99 b.uint16(h.Method)
100 b.uint16(h.ModifiedTime)
101 b.uint16(h.ModifiedDate)
102 b.uint32(h.CRC32)
103 if h.isZip64() || h.offset >= uint32max {
104
105
106
107 b.uint32(uint32max)
108 b.uint32(uint32max)
109
110
111 var buf [28]byte
112 eb := writeBuf(buf[:])
113 eb.uint16(zip64ExtraID)
114 eb.uint16(24)
115 eb.uint64(h.UncompressedSize64)
116 eb.uint64(h.CompressedSize64)
117 eb.uint64(h.offset)
118 h.Extra = append(h.Extra, buf[:]...)
119 } else {
120 b.uint32(h.CompressedSize)
121 b.uint32(h.UncompressedSize)
122 }
123
124 b.uint16(uint16(len(h.Name)))
125 b.uint16(uint16(len(h.Extra)))
126 b.uint16(uint16(len(h.Comment)))
127 b = b[4:]
128 b.uint32(h.ExternalAttrs)
129 if h.offset > uint32max {
130 b.uint32(uint32max)
131 } else {
132 b.uint32(uint32(h.offset))
133 }
134 if _, err := w.cw.Write(buf[:]); err != nil {
135 return err
136 }
137 if _, err := io.WriteString(w.cw, h.Name); err != nil {
138 return err
139 }
140 if _, err := w.cw.Write(h.Extra); err != nil {
141 return err
142 }
143 if _, err := io.WriteString(w.cw, h.Comment); err != nil {
144 return err
145 }
146 }
147 end := w.cw.count
148
149 records := uint64(len(w.dir))
150 size := uint64(end - start)
151 offset := uint64(start)
152
153 if f := w.testHookCloseSizeOffset; f != nil {
154 f(size, offset)
155 }
156
157 if records >= uint16max || size >= uint32max || offset >= uint32max {
158 var buf [directory64EndLen + directory64LocLen]byte
159 b := writeBuf(buf[:])
160
161
162 b.uint32(directory64EndSignature)
163 b.uint64(directory64EndLen - 12)
164 b.uint16(zipVersion45)
165 b.uint16(zipVersion45)
166 b.uint32(0)
167 b.uint32(0)
168 b.uint64(records)
169 b.uint64(records)
170 b.uint64(size)
171 b.uint64(offset)
172
173
174 b.uint32(directory64LocSignature)
175 b.uint32(0)
176 b.uint64(uint64(end))
177 b.uint32(1)
178
179 if _, err := w.cw.Write(buf[:]); err != nil {
180 return err
181 }
182
183
184
185 records = uint16max
186 size = uint32max
187 offset = uint32max
188 }
189
190
191 var buf [directoryEndLen]byte
192 b := writeBuf(buf[:])
193 b.uint32(uint32(directoryEndSignature))
194 b = b[4:]
195 b.uint16(uint16(records))
196 b.uint16(uint16(records))
197 b.uint32(uint32(size))
198 b.uint32(uint32(offset))
199 b.uint16(uint16(len(w.comment)))
200 if _, err := w.cw.Write(buf[:]); err != nil {
201 return err
202 }
203 if _, err := io.WriteString(w.cw, w.comment); err != nil {
204 return err
205 }
206
207 return w.cw.w.(*bufio.Writer).Flush()
208 }
209
210
211
212
213
214
215
216
217
218
219
220 func (w *Writer) Create(name string) (io.Writer, error) {
221 header := &FileHeader{
222 Name: name,
223 Method: Deflate,
224 }
225 return w.CreateHeader(header)
226 }
227
228
229
230
231 func detectUTF8(s string) (valid, require bool) {
232 for i := 0; i < len(s); {
233 r, size := utf8.DecodeRuneInString(s[i:])
234 i += size
235
236
237
238
239
240
241 if r < 0x20 || r > 0x7d || r == 0x5c {
242 if !utf8.ValidRune(r) || (r == utf8.RuneError && size == 1) {
243 return false, false
244 }
245 require = true
246 }
247 }
248 return true, require
249 }
250
251
252
253 func (w *Writer) prepare(fh *FileHeader) error {
254 if w.last != nil && !w.last.closed {
255 if err := w.last.close(); err != nil {
256 return err
257 }
258 }
259 if len(w.dir) > 0 && w.dir[len(w.dir)-1].FileHeader == fh {
260
261 return errors.New("archive/zip: invalid duplicate FileHeader")
262 }
263 return nil
264 }
265
266
267
268
269
270
271
272
273 func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
274 if err := w.prepare(fh); err != nil {
275 return nil, err
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294 utf8Valid1, utf8Require1 := detectUTF8(fh.Name)
295 utf8Valid2, utf8Require2 := detectUTF8(fh.Comment)
296 switch {
297 case fh.NonUTF8:
298 fh.Flags &^= 0x800
299 case (utf8Require1 || utf8Require2) && (utf8Valid1 && utf8Valid2):
300 fh.Flags |= 0x800
301 }
302
303 fh.CreatorVersion = fh.CreatorVersion&0xff00 | zipVersion20
304 fh.ReaderVersion = zipVersion20
305
306
307 if !fh.Modified.IsZero() {
308
309
310
311
312
313
314
315
316 fh.ModifiedDate, fh.ModifiedTime = timeToMsDosTime(fh.Modified)
317
318
319
320
321
322
323
324 var mbuf [9]byte
325 mt := uint32(fh.Modified.Unix())
326 eb := writeBuf(mbuf[:])
327 eb.uint16(extTimeExtraID)
328 eb.uint16(5)
329 eb.uint8(1)
330 eb.uint32(mt)
331 fh.Extra = append(fh.Extra, mbuf[:]...)
332 }
333
334 var (
335 ow io.Writer
336 fw *fileWriter
337 )
338 h := &header{
339 FileHeader: fh,
340 offset: uint64(w.cw.count),
341 }
342
343 if strings.HasSuffix(fh.Name, "/") {
344
345
346
347
348 fh.Method = Store
349 fh.Flags &^= 0x8
350
351
352 fh.CompressedSize = 0
353 fh.CompressedSize64 = 0
354 fh.UncompressedSize = 0
355 fh.UncompressedSize64 = 0
356
357 ow = dirWriter{}
358 } else {
359 fh.Flags |= 0x8
360
361 fw = &fileWriter{
362 zipw: w.cw,
363 compCount: &countWriter{w: w.cw},
364 crc32: crc32.NewIEEE(),
365 }
366 comp := w.compressor(fh.Method)
367 if comp == nil {
368 return nil, ErrAlgorithm
369 }
370 var err error
371 fw.comp, err = comp(fw.compCount)
372 if err != nil {
373 return nil, err
374 }
375 fw.rawCount = &countWriter{w: fw.comp}
376 fw.header = h
377 ow = fw
378 }
379 w.dir = append(w.dir, h)
380 if err := writeHeader(w.cw, h); err != nil {
381 return nil, err
382 }
383
384 w.last = fw
385 return ow, nil
386 }
387
388 func writeHeader(w io.Writer, h *header) error {
389 const maxUint16 = 1<<16 - 1
390 if len(h.Name) > maxUint16 {
391 return errLongName
392 }
393 if len(h.Extra) > maxUint16 {
394 return errLongExtra
395 }
396
397 var buf [fileHeaderLen]byte
398 b := writeBuf(buf[:])
399 b.uint32(uint32(fileHeaderSignature))
400 b.uint16(h.ReaderVersion)
401 b.uint16(h.Flags)
402 b.uint16(h.Method)
403 b.uint16(h.ModifiedTime)
404 b.uint16(h.ModifiedDate)
405
406
407
408 if h.raw && !h.hasDataDescriptor() {
409 b.uint32(h.CRC32)
410 b.uint32(uint32(min(h.CompressedSize64, uint32max)))
411 b.uint32(uint32(min(h.UncompressedSize64, uint32max)))
412 } else {
413
414
415 b.uint32(0)
416 b.uint32(0)
417 b.uint32(0)
418 }
419 b.uint16(uint16(len(h.Name)))
420 b.uint16(uint16(len(h.Extra)))
421 if _, err := w.Write(buf[:]); err != nil {
422 return err
423 }
424 if _, err := io.WriteString(w, h.Name); err != nil {
425 return err
426 }
427 _, err := w.Write(h.Extra)
428 return err
429 }
430
431
432
433
434
435
436
437
438
439
440
441 func (w *Writer) CreateRaw(fh *FileHeader) (io.Writer, error) {
442 if err := w.prepare(fh); err != nil {
443 return nil, err
444 }
445
446 fh.CompressedSize = uint32(min(fh.CompressedSize64, uint32max))
447 fh.UncompressedSize = uint32(min(fh.UncompressedSize64, uint32max))
448
449 h := &header{
450 FileHeader: fh,
451 offset: uint64(w.cw.count),
452 raw: true,
453 }
454 w.dir = append(w.dir, h)
455 if err := writeHeader(w.cw, h); err != nil {
456 return nil, err
457 }
458
459 if strings.HasSuffix(fh.Name, "/") {
460 w.last = nil
461 return dirWriter{}, nil
462 }
463
464 fw := &fileWriter{
465 header: h,
466 zipw: w.cw,
467 }
468 w.last = fw
469 return fw, nil
470 }
471
472
473
474 func (w *Writer) Copy(f *File) error {
475 r, err := f.OpenRaw()
476 if err != nil {
477 return err
478 }
479
480
481 fh := f.FileHeader
482 fw, err := w.CreateRaw(&fh)
483 if err != nil {
484 return err
485 }
486 _, err = io.Copy(fw, r)
487 return err
488 }
489
490
491
492
493 func (w *Writer) RegisterCompressor(method uint16, comp Compressor) {
494 if w.compressors == nil {
495 w.compressors = make(map[uint16]Compressor)
496 }
497 w.compressors[method] = comp
498 }
499
500
501
502
503 func (w *Writer) AddFS(fsys fs.FS) error {
504 return fs.WalkDir(fsys, ".", func(name string, d fs.DirEntry, err error) error {
505 if err != nil {
506 return err
507 }
508 if d.IsDir() {
509 return nil
510 }
511 info, err := d.Info()
512 if err != nil {
513 return err
514 }
515 if !info.Mode().IsRegular() {
516 return errors.New("zip: cannot add non-regular file")
517 }
518 h, err := FileInfoHeader(info)
519 if err != nil {
520 return err
521 }
522 h.Name = name
523 h.Method = Deflate
524 fw, err := w.CreateHeader(h)
525 if err != nil {
526 return err
527 }
528 f, err := fsys.Open(name)
529 if err != nil {
530 return err
531 }
532 defer f.Close()
533 _, err = io.Copy(fw, f)
534 return err
535 })
536 }
537
538 func (w *Writer) compressor(method uint16) Compressor {
539 comp := w.compressors[method]
540 if comp == nil {
541 comp = compressor(method)
542 }
543 return comp
544 }
545
546 type dirWriter struct{}
547
548 func (dirWriter) Write(b []byte) (int, error) {
549 if len(b) == 0 {
550 return 0, nil
551 }
552 return 0, errors.New("zip: write to directory")
553 }
554
555 type fileWriter struct {
556 *header
557 zipw io.Writer
558 rawCount *countWriter
559 comp io.WriteCloser
560 compCount *countWriter
561 crc32 hash.Hash32
562 closed bool
563 }
564
565 func (w *fileWriter) Write(p []byte) (int, error) {
566 if w.closed {
567 return 0, errors.New("zip: write to closed file")
568 }
569 if w.raw {
570 return w.zipw.Write(p)
571 }
572 w.crc32.Write(p)
573 return w.rawCount.Write(p)
574 }
575
576 func (w *fileWriter) close() error {
577 if w.closed {
578 return errors.New("zip: file closed twice")
579 }
580 w.closed = true
581 if w.raw {
582 return w.writeDataDescriptor()
583 }
584 if err := w.comp.Close(); err != nil {
585 return err
586 }
587
588
589 fh := w.header.FileHeader
590 fh.CRC32 = w.crc32.Sum32()
591 fh.CompressedSize64 = uint64(w.compCount.count)
592 fh.UncompressedSize64 = uint64(w.rawCount.count)
593
594 if fh.isZip64() {
595 fh.CompressedSize = uint32max
596 fh.UncompressedSize = uint32max
597 fh.ReaderVersion = zipVersion45
598 } else {
599 fh.CompressedSize = uint32(fh.CompressedSize64)
600 fh.UncompressedSize = uint32(fh.UncompressedSize64)
601 }
602
603 return w.writeDataDescriptor()
604 }
605
606 func (w *fileWriter) writeDataDescriptor() error {
607 if !w.hasDataDescriptor() {
608 return nil
609 }
610
611
612
613
614
615 var buf []byte
616 if w.isZip64() {
617 buf = make([]byte, dataDescriptor64Len)
618 } else {
619 buf = make([]byte, dataDescriptorLen)
620 }
621 b := writeBuf(buf)
622 b.uint32(dataDescriptorSignature)
623 b.uint32(w.CRC32)
624 if w.isZip64() {
625 b.uint64(w.CompressedSize64)
626 b.uint64(w.UncompressedSize64)
627 } else {
628 b.uint32(w.CompressedSize)
629 b.uint32(w.UncompressedSize)
630 }
631 _, err := w.zipw.Write(buf)
632 return err
633 }
634
635 type countWriter struct {
636 w io.Writer
637 count int64
638 }
639
640 func (w *countWriter) Write(p []byte) (int, error) {
641 n, err := w.w.Write(p)
642 w.count += int64(n)
643 return n, err
644 }
645
646 type nopCloser struct {
647 io.Writer
648 }
649
650 func (w nopCloser) Close() error {
651 return nil
652 }
653
654 type writeBuf []byte
655
656 func (b *writeBuf) uint8(v uint8) {
657 (*b)[0] = v
658 *b = (*b)[1:]
659 }
660
661 func (b *writeBuf) uint16(v uint16) {
662 binary.LittleEndian.PutUint16(*b, v)
663 *b = (*b)[2:]
664 }
665
666 func (b *writeBuf) uint32(v uint32) {
667 binary.LittleEndian.PutUint32(*b, v)
668 *b = (*b)[4:]
669 }
670
671 func (b *writeBuf) uint64(v uint64) {
672 binary.LittleEndian.PutUint64(*b, v)
673 *b = (*b)[8:]
674 }
675
View as plain text