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