github.com/sercand/please@v13.4.0+incompatible/third_party/go/zip/writer.go (about) 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 5 package zip 6 7 import ( 8 "bufio" 9 "encoding/binary" 10 "errors" 11 "hash" 12 "hash/crc32" 13 "io" 14 ) 15 16 // TODO(adg): support zip file comments 17 // TODO(adg): support specifying deflate level 18 19 // Writer implements a zip file writer. 20 type Writer struct { 21 cw *countWriter 22 dir []*header 23 last *fileWriter 24 closed bool 25 } 26 27 type header struct { 28 *FileHeader 29 offset uint64 30 } 31 32 // NewWriter returns a new Writer writing a zip file to w. 33 func NewWriter(w io.Writer) *Writer { 34 return &Writer{cw: &countWriter{w: bufio.NewWriter(w)}} 35 } 36 37 // Flush flushes any buffered data to the underlying writer. 38 // Calling Flush is not normally necessary; calling Close is sufficient. 39 func (w *Writer) Flush() error { 40 return w.cw.w.(*bufio.Writer).Flush() 41 } 42 43 // Close finishes writing the zip file by writing the central directory. 44 // It does not (and can not) close the underlying writer. 45 func (w *Writer) Close() error { 46 if w.last != nil && !w.last.closed { 47 if err := w.last.close(); err != nil { 48 return err 49 } 50 w.last = nil 51 } 52 if w.closed { 53 return errors.New("zip: writer closed twice") 54 } 55 w.closed = true 56 57 // write central directory 58 start := w.cw.count 59 for _, h := range w.dir { 60 var buf [directoryHeaderLen]byte 61 b := writeBuf(buf[:]) 62 b.uint32(uint32(directoryHeaderSignature)) 63 b.uint16(h.CreatorVersion) 64 b.uint16(h.ReaderVersion) 65 b.uint16(h.Flags) 66 b.uint16(h.Method) 67 b.uint16(h.ModifiedTime) 68 b.uint16(h.ModifiedDate) 69 b.uint32(h.CRC32) 70 if h.isZip64() || h.offset > uint32max { 71 // the file needs a zip64 header. store maxint in both 72 // 32 bit size fields (and offset later) to signal that the 73 // zip64 extra header should be used. 74 b.uint32(uint32max) // compressed size 75 b.uint32(uint32max) // uncompressed size 76 77 // append a zip64 extra block to Extra 78 var buf [28]byte // 2x uint16 + 3x uint64 79 eb := writeBuf(buf[:]) 80 eb.uint16(zip64ExtraId) 81 eb.uint16(24) // size = 3x uint64 82 eb.uint64(h.UncompressedSize64) 83 eb.uint64(h.CompressedSize64) 84 eb.uint64(h.offset) 85 h.Extra = append(h.Extra, buf[:]...) 86 } else { 87 b.uint32(h.CompressedSize) 88 b.uint32(h.UncompressedSize) 89 } 90 b.uint16(uint16(len(h.Name))) 91 b.uint16(uint16(len(h.Extra))) 92 b.uint16(uint16(len(h.Comment))) 93 b = b[4:] // skip disk number start and internal file attr (2x uint16) 94 b.uint32(h.ExternalAttrs) 95 if h.offset > uint32max { 96 b.uint32(uint32max) 97 } else { 98 b.uint32(uint32(h.offset)) 99 } 100 if _, err := w.cw.Write(buf[:]); err != nil { 101 return err 102 } 103 if _, err := io.WriteString(w.cw, h.Name); err != nil { 104 return err 105 } 106 if _, err := w.cw.Write(h.Extra); err != nil { 107 return err 108 } 109 if _, err := io.WriteString(w.cw, h.Comment); err != nil { 110 return err 111 } 112 } 113 end := w.cw.count 114 115 records := uint64(len(w.dir)) 116 size := uint64(end - start) 117 offset := uint64(start) 118 119 if records > uint16max || size > uint32max || offset > uint32max { 120 var buf [directory64EndLen + directory64LocLen]byte 121 b := writeBuf(buf[:]) 122 123 // zip64 end of central directory record 124 b.uint32(directory64EndSignature) 125 b.uint64(directory64EndLen) 126 b.uint16(zipVersion45) // version made by 127 b.uint16(zipVersion45) // version needed to extract 128 b.uint32(0) // number of this disk 129 b.uint32(0) // number of the disk with the start of the central directory 130 b.uint64(records) // total number of entries in the central directory on this disk 131 b.uint64(records) // total number of entries in the central directory 132 b.uint64(size) // size of the central directory 133 b.uint64(offset) // offset of start of central directory with respect to the starting disk number 134 135 // zip64 end of central directory locator 136 b.uint32(directory64LocSignature) 137 b.uint32(0) // number of the disk with the start of the zip64 end of central directory 138 b.uint64(uint64(end)) // relative offset of the zip64 end of central directory record 139 b.uint32(1) // total number of disks 140 141 if _, err := w.cw.Write(buf[:]); err != nil { 142 return err 143 } 144 145 // store max values in the regular end record to signal that 146 // that the zip64 values should be used instead 147 records = uint16max 148 size = uint32max 149 offset = uint32max 150 } 151 152 // write end record 153 var buf [directoryEndLen]byte 154 b := writeBuf(buf[:]) 155 b.uint32(uint32(directoryEndSignature)) 156 b = b[4:] // skip over disk number and first disk number (2x uint16) 157 b.uint16(uint16(records)) // number of entries this disk 158 b.uint16(uint16(records)) // number of entries total 159 b.uint32(uint32(size)) // size of directory 160 b.uint32(uint32(offset)) // start of directory 161 // skipped size of comment (always zero) 162 if _, err := w.cw.Write(buf[:]); err != nil { 163 return err 164 } 165 166 return w.cw.w.(*bufio.Writer).Flush() 167 } 168 169 // WriteRaw writes some data to the file, not part of any of the internal structure. 170 // This is useful for preambles; since zip files have their index at the end and 171 // file locations are specified as offsets it's possible to lead off with something, for 172 // example a shebang to make them self-executing as with pexes. 173 func (w *Writer) WriteRaw(b []byte) error { 174 _, err := w.cw.Write(b) 175 return err 176 } 177 178 // Offset returns the offset that the next file would begin at. 179 func (w *Writer) Offset() int { 180 return int(w.cw.count) 181 } 182 183 // Create adds a file to the zip file using the provided name. 184 // It returns a Writer to which the file contents should be written. 185 // The name must be a relative path: it must not start with a drive 186 // letter (e.g. C:) or leading slash, and only forward slashes are 187 // allowed. 188 // The file's contents must be written to the io.Writer before the next 189 // call to Create, CreateHeader, or Close. 190 func (w *Writer) Create(name string) (io.Writer, error) { 191 header := &FileHeader{ 192 Name: name, 193 Method: Deflate, 194 } 195 return w.CreateHeader(header) 196 } 197 198 // CreateHeader adds a file to the zip file using the provided FileHeader 199 // for the file metadata. 200 // It returns a Writer to which the file contents should be written. 201 // The file's contents must be written to the io.Writer before the next 202 // call to Create, CreateHeader, or Close. 203 func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) { 204 comp := compressor(fh.Method) 205 if comp == nil { 206 return nil, ErrAlgorithm 207 } 208 if fh.Method != Store { 209 fh.Flags |= dataDescriptorFlag // we will write a data descriptor 210 } 211 return w.CreateHeaderWithCompressor(fh, comp, crc32.NewIEEE()) 212 } 213 214 // CreateHeaderWithCompressor behaves the same as CreateHeader but allows explicitly 215 // specifying the compressor to use, which may not necessarily correspond to the 216 // Method property of the actual file header. Some care should therefore be taken 217 // to ensure the zipfile will be decompressible if calling this directly. 218 func (w *Writer) CreateHeaderWithCompressor(fh *FileHeader, comp Compressor, crc hash.Hash32) (io.Writer, error) { 219 if w.last != nil && !w.last.closed { 220 if err := w.last.close(); err != nil { 221 return nil, err 222 } 223 } 224 225 fh.CreatorVersion = fh.CreatorVersion&0xff00 | zipVersion20 // preserve compatibility byte 226 fh.ReaderVersion = zipVersion20 227 228 fw := &fileWriter{ 229 zipw: w.cw, 230 compCount: &countWriter{w: w.cw}, 231 crc32: crc, 232 } 233 var err error 234 fw.comp, err = comp(fw.compCount) 235 if err != nil { 236 return nil, err 237 } 238 fw.rawCount = &countWriter{w: fw.comp} 239 240 h := &header{ 241 FileHeader: fh, 242 offset: uint64(w.cw.count), 243 } 244 w.dir = append(w.dir, h) 245 fw.header = h 246 247 if err := writeHeader(w.cw, fh); err != nil { 248 return nil, err 249 } 250 251 w.last = fw 252 return fw, nil 253 } 254 255 func writeHeader(w io.Writer, h *FileHeader) error { 256 var buf [fileHeaderLen]byte 257 b := writeBuf(buf[:]) 258 b.uint32(uint32(fileHeaderSignature)) 259 b.uint16(h.ReaderVersion) 260 b.uint16(h.Flags) 261 b.uint16(h.Method) 262 b.uint16(h.ModifiedTime) 263 b.uint16(h.ModifiedDate) 264 if (h.Flags & dataDescriptorFlag) != 0 { 265 b.uint32(0) // since we are writing a data descriptor crc32, 266 b.uint32(0) // compressed size, 267 b.uint32(0) // and uncompressed size should be zero 268 } else { 269 b.uint32(h.CRC32) 270 b.uint32(h.CompressedSize) 271 b.uint32(h.UncompressedSize) 272 } 273 b.uint16(uint16(len(h.Name))) 274 b.uint16(uint16(len(h.Extra))) 275 if _, err := w.Write(buf[:]); err != nil { 276 return err 277 } 278 if _, err := io.WriteString(w, h.Name); err != nil { 279 return err 280 } 281 _, err := w.Write(h.Extra) 282 return err 283 } 284 285 type fileWriter struct { 286 *header 287 zipw io.Writer 288 rawCount *countWriter 289 comp io.WriteCloser 290 compCount *countWriter 291 crc32 hash.Hash32 292 closed bool 293 } 294 295 func (w *fileWriter) Write(p []byte) (int, error) { 296 if w.closed { 297 return 0, errors.New("zip: write to closed file") 298 } 299 w.crc32.Write(p) 300 return w.rawCount.Write(p) 301 } 302 303 func (w *fileWriter) close() error { 304 if w.closed { 305 return errors.New("zip: file closed twice") 306 } 307 w.closed = true 308 if err := w.comp.Close(); err != nil { 309 return err 310 } 311 312 // update FileHeader 313 fh := w.header.FileHeader 314 if (fh.Flags & dataDescriptorFlag) == 0 { 315 return nil 316 } 317 fh.CRC32 = w.crc32.Sum32() 318 fh.CompressedSize64 = uint64(w.compCount.count) 319 fh.UncompressedSize64 = uint64(w.rawCount.count) 320 321 if fh.isZip64() { 322 fh.CompressedSize = uint32max 323 fh.UncompressedSize = uint32max 324 fh.ReaderVersion = zipVersion45 // requires 4.5 - File uses ZIP64 format extensions 325 } else { 326 fh.CompressedSize = uint32(fh.CompressedSize64) 327 fh.UncompressedSize = uint32(fh.UncompressedSize64) 328 } 329 330 // Write data descriptor. This is more complicated than one would 331 // think, see e.g. comments in zipfile.c:putextended() and 332 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073588. 333 // The approach here is to write 8 byte sizes if needed without 334 // adding a zip64 extra in the local header (too late anyway). 335 var buf []byte 336 if fh.isZip64() { 337 buf = make([]byte, dataDescriptor64Len) 338 } else { 339 buf = make([]byte, dataDescriptorLen) 340 } 341 b := writeBuf(buf) 342 b.uint32(dataDescriptorSignature) // de-facto standard, required by OS X 343 b.uint32(fh.CRC32) 344 if fh.isZip64() { 345 b.uint64(fh.CompressedSize64) 346 b.uint64(fh.UncompressedSize64) 347 } else { 348 b.uint32(fh.CompressedSize) 349 b.uint32(fh.UncompressedSize) 350 } 351 _, err := w.zipw.Write(buf) 352 return err 353 } 354 355 type countWriter struct { 356 w io.Writer 357 count int64 358 } 359 360 func (w *countWriter) Write(p []byte) (int, error) { 361 n, err := w.w.Write(p) 362 w.count += int64(n) 363 return n, err 364 } 365 366 type nopCloser struct { 367 io.Writer 368 } 369 370 func (w nopCloser) Close() error { 371 return nil 372 } 373 374 type writeBuf []byte 375 376 func (b *writeBuf) uint16(v uint16) { 377 binary.LittleEndian.PutUint16(*b, v) 378 *b = (*b)[2:] 379 } 380 381 func (b *writeBuf) uint32(v uint32) { 382 binary.LittleEndian.PutUint32(*b, v) 383 *b = (*b)[4:] 384 } 385 386 func (b *writeBuf) uint64(v uint64) { 387 binary.LittleEndian.PutUint64(*b, v) 388 *b = (*b)[8:] 389 }