archive/zip: fix regression when writing directories

Several adjustments:

1) When encoding the FileHeader for a directory, explicitly set all of the sizes
to zero regardless of their prior values. These values are currently populated
by FileInfoHeader as it calls os.FileInfo.Size regardless of whether the file is
a directory or not. We avoid fixing FileInfoHeader now as it is too late in the
release cycle (see #24082).

We silently adjust slightly wrong FileHeader fields as opposed to returning
an error because the CreateHeader method already does such mutations
(e.g., for UTF-8 detection, data descriptor, etc).

2) Have dirWriter.Write only return an error if some number of bytes are written.
Some code still call Write for both normal files and directories, but just pass
an empty []byte to Write for directories.

Change-Id: I85492a31356107fcf76dc89ceb00a28853754289
Reviewed-on: https://go-review.googlesource.com/124155
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/src/archive/zip/writer.go b/src/archive/zip/writer.go
index 506148e..5f0c0a1 100644
--- a/src/archive/zip/writer.go
+++ b/src/archive/zip/writer.go
@@ -336,6 +336,12 @@
 		fh.Method = Store
 		fh.Flags &^= 0x8 // we will not write a data descriptor
 
+		// Explicitly clear sizes as they have no meaning for directories.
+		fh.CompressedSize = 0
+		fh.CompressedSize64 = 0
+		fh.UncompressedSize = 0
+		fh.UncompressedSize64 = 0
+
 		ow = dirWriter{}
 	} else {
 		fh.Flags |= 0x8 // we will write a data descriptor
@@ -419,7 +425,10 @@
 
 type dirWriter struct{}
 
-func (dirWriter) Write([]byte) (int, error) {
+func (dirWriter) Write(b []byte) (int, error) {
+	if len(b) == 0 {
+		return 0, nil
+	}
 	return 0, errors.New("zip: write to directory")
 }
 
diff --git a/src/archive/zip/writer_test.go b/src/archive/zip/writer_test.go
index 468424c..1fedfd8 100644
--- a/src/archive/zip/writer_test.go
+++ b/src/archive/zip/writer_test.go
@@ -306,21 +306,28 @@
 	if err != nil {
 		t.Fatal(err)
 	}
+	if _, err := dw.Write(nil); err != nil {
+		t.Errorf("Write(nil) to directory: got %v, want nil", err)
+	}
 	if _, err := dw.Write([]byte("hello")); err == nil {
-		t.Error("Write to directory: got nil error, want non-nil")
+		t.Error(`Write("hello") to directory: got nil error, want non-nil`)
 	}
 }
 
 func TestWriterDirAttributes(t *testing.T) {
 	var buf bytes.Buffer
 	w := NewWriter(&buf)
-	if _, err := w.Create("dir/"); err != nil {
+	if _, err := w.CreateHeader(&FileHeader{
+		Name:               "dir/",
+		Method:             Deflate,
+		CompressedSize64:   1234,
+		UncompressedSize64: 5678,
+	}); err != nil {
 		t.Fatal(err)
 	}
 	if err := w.Close(); err != nil {
 		t.Fatal(err)
 	}
-
 	b := buf.Bytes()
 
 	var sig [4]byte