summaryrefslogtreecommitdiff
path: root/src/pkg
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2014-08-31 21:32:13 -0700
committerBrad Fitzpatrick <bradfitz@golang.org>2014-08-31 21:32:13 -0700
commit8974473626cfa8566b48bb2dcf736e3c183c7558 (patch)
tree948dd83196482750a90f878daa8936c360391fab /src/pkg
parentc2f42a46c8135dfc0e66b26c949daf38e789f4a5 (diff)
downloadgo-8974473626cfa8566b48bb2dcf736e3c183c7558.tar.gz
archive/zip: add Writer.Flush
This is needed for callers to be able to keep track of the writing position within a zip file. Otherwise it's not possible to compute the size of headers, and the TOC isn't written until the very end. LGTM=adg R=adg CC=golang-codereviews https://codereview.appspot.com/134210043
Diffstat (limited to 'src/pkg')
-rw-r--r--src/pkg/archive/zip/writer.go6
-rw-r--r--src/pkg/archive/zip/writer_test.go19
2 files changed, 25 insertions, 0 deletions
diff --git a/src/pkg/archive/zip/writer.go b/src/pkg/archive/zip/writer.go
index 6c9800a78..170beec0e 100644
--- a/src/pkg/archive/zip/writer.go
+++ b/src/pkg/archive/zip/writer.go
@@ -34,6 +34,12 @@ func NewWriter(w io.Writer) *Writer {
return &Writer{cw: &countWriter{w: bufio.NewWriter(w)}}
}
+// Flush flushes any buffered data to the underlying writer.
+// Calling Flush is not normally necessary; calling Close is sufficient.
+func (w *Writer) Flush() error {
+ return w.cw.w.(*bufio.Writer).Flush()
+}
+
// Close finishes writing the zip file by writing the central directory.
// It does not (and can not) close the underlying writer.
func (w *Writer) Close() error {
diff --git a/src/pkg/archive/zip/writer_test.go b/src/pkg/archive/zip/writer_test.go
index 4bfa87080..184a7d96a 100644
--- a/src/pkg/archive/zip/writer_test.go
+++ b/src/pkg/archive/zip/writer_test.go
@@ -6,6 +6,7 @@ package zip
import (
"bytes"
+ "io"
"io/ioutil"
"math/rand"
"os"
@@ -86,6 +87,24 @@ func TestWriter(t *testing.T) {
}
}
+func TestWriterFlush(t *testing.T) {
+ var buf bytes.Buffer
+ w := NewWriter(struct{ io.Writer }{&buf})
+ _, err := w.Create("foo")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if buf.Len() > 0 {
+ t.Fatalf("Unexpected %d bytes already in buffer", buf.Len())
+ }
+ if err := w.Flush(); err != nil {
+ t.Fatal(err)
+ }
+ if buf.Len() == 0 {
+ t.Fatal("No bytes written after Flush")
+ }
+}
+
func testCreate(t *testing.T, w *Writer, wt *WriteTest) {
header := &FileHeader{
Name: wt.Name,