last *fileWriter
closed bool
compressors map[uint16]Compressor
+ comment string
// testHookCloseSizeOffset if non-nil is called with the size
// of offset of the central directory at Close.
testHookCloseSizeOffset func(size, offset uint64)
-
- // Comment is the central directory comment and must be set before Close is called.
- Comment string
}
type header struct {
return w.cw.w.(*bufio.Writer).Flush()
}
-// Close finishes writing the zip file by writing the central directory.
-// It does not (and cannot) close the underlying writer.
-func (w *Writer) Close() error {
- if len(w.Comment) > uint16max {
+// SetComment sets the end-of-central-directory comment field.
+// It can only be called before Close.
+func (w *Writer) SetComment(comment string) error {
+ if len(comment) > uint16max {
return errors.New("zip: Writer.Comment too long")
}
+ w.comment = comment
+ return nil
+}
+// Close finishes writing the zip file by writing the central directory.
+// It does not (and cannot) close the underlying writer.
+func (w *Writer) Close() error {
if w.last != nil && !w.last.closed {
if err := w.last.close(); err != nil {
return err
b.uint16(uint16(records)) // number of entries total
b.uint32(uint32(size)) // size of directory
b.uint32(uint32(offset)) // start of directory
- b.uint16(uint16(len(w.Comment))) // byte size of EOCD comment
+ b.uint16(uint16(len(w.comment))) // byte size of EOCD comment
if _, err := w.cw.Write(buf[:]); err != nil {
return err
}
- if _, err := io.WriteString(w.cw, w.Comment); err != nil {
+ if _, err := io.WriteString(w.cw, w.comment); err != nil {
return err
}
// write a zip file
buf := new(bytes.Buffer)
w := NewWriter(buf)
- w.Comment = test.comment
+ if err := w.SetComment(test.comment); err != nil {
+ if test.ok {
+ t.Fatalf("SetComment: unexpected error %v", err)
+ }
+ continue
+ } else {
+ if !test.ok {
+ t.Fatalf("SetComment: unexpected success, want error")
+ }
+ }
if err := w.Close(); test.ok == (err != nil) {
t.Fatal(err)