From fcf52b8e4835d883a545aa876d29b1b00f48ed71 Mon Sep 17 00:00:00 2001 From: Arran Walker Date: Sun, 7 Jun 2020 01:56:22 +0000 Subject: [PATCH] zip: update CreateHeaderRaw to handle zip64 fields. --- zip/writer.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/zip/writer.go b/zip/writer.go index 4d0628a879..335b637c8f 100644 --- a/zip/writer.go +++ b/zip/writer.go @@ -218,7 +218,7 @@ func (w *Writer) Close() error { // allowed. To create a directory instead of a file, add a trailing // slash to the name. // The file's contents must be written to the io.Writer before the next -// call to Create, CreateHeader, or Close. +// call to Create, CreateHeader, CreateHeaderRaw, or Close. func (w *Writer) Create(name string) (io.Writer, error) { header := &FileHeader{ Name: name, @@ -403,8 +403,10 @@ func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) { // The file's contents must be written to the io.Writer before the next // call to Create, Copy, CreateHeader, CreateHeaderRaw or Close. // -// Using this requires knowledge of populating the FileHeader correctly. -// Generally using the Copy() function is recommended. +// Using this requires knowledge of populating the FileHeader correctly (the +// UncompressedSize64 and CRC32 fields should be set and valid for the contents +// written). For copying from an existing zip file, the Copy() function is +// recommended. func (w *Writer) CreateHeaderRaw(fh *FileHeader) (io.Writer, error) { if w.last != nil && !w.last.Closed() { if err := w.last.Close(); err != nil { @@ -669,6 +671,15 @@ func (w *rawWriter) Close() error { fh := w.FileHeader fh.CompressedSize64 = uint64(w.rawCount.count) + if fh.isZip64() { + fh.CompressedSize = uint32max + fh.UncompressedSize = uint32max + fh.ReaderVersion = zipVersion45 // requires 4.5 - File uses ZIP64 format extensions + } else { + fh.CompressedSize = uint32(fh.CompressedSize64) + fh.UncompressedSize = uint32(fh.UncompressedSize64) + } + // Write data descriptor. This is more complicated than one would // think, see e.g. comments in zipfile.c:putextended() and // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073588.