Support writing a ZIP64 file header

If the length of a stored file is more than 2^32 and a data descriptor
is not being used then a ZIP64 extra is required in the file header to
store the uncompressed and compressed lengths.

Bug: 175055267
Test: TestCopyFromZip64
Change-Id: Id414b4c04f48aefabfd835bd8339333d36576375
This commit is contained in:
Colin Cross
2020-12-17 15:08:01 -08:00
parent f1c48afc31
commit 373147baa9
2 changed files with 81 additions and 3 deletions

View File

@@ -276,9 +276,6 @@ func writeHeader(w io.Writer, h *FileHeader) error {
} else {
b.uint32(h.CRC32)
if h.CompressedSize64 > uint32max || h.UncompressedSize64 > uint32max {
panic("skipping writing the data descriptor for a 64-bit value is not yet supported")
}
compressedSize := uint32(h.CompressedSize64)
if compressedSize == 0 {
compressedSize = h.CompressedSize
@@ -289,6 +286,21 @@ func writeHeader(w io.Writer, h *FileHeader) error {
uncompressedSize = h.UncompressedSize
}
if h.CompressedSize64 > uint32max || h.UncompressedSize64 > uint32max {
// Sizes don't fit in a 32-bit field, put them in a zip64 extra instead.
compressedSize = uint32max
uncompressedSize = uint32max
// append a zip64 extra block to Extra
var buf [20]byte // 2x uint16 + 2x uint64
eb := writeBuf(buf[:])
eb.uint16(zip64ExtraId)
eb.uint16(16) // size = 2x uint64
eb.uint64(h.UncompressedSize64)
eb.uint64(h.CompressedSize64)
h.Extra = append(h.Extra, buf[:]...)
}
b.uint32(compressedSize)
b.uint32(uncompressedSize)
}