]> Cypherpunks repositories - gostls13.git/commitdiff
archive/zip: remove WriterOptions and replace with SetOffset method
authorAndrew Gerrand <adg@golang.org>
Thu, 12 Mar 2015 00:54:11 +0000 (11:54 +1100)
committerAndrew Gerrand <adg@golang.org>
Thu, 12 Mar 2015 21:32:09 +0000 (21:32 +0000)
Change-Id: I0a8b972c33e80c750ff1d63717177a5a3294a112
Reviewed-on: https://go-review.googlesource.com/7445
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Geert-Johan Riemer <gjr19912@gmail.com>
Reviewed-by: Andrew Gerrand <adg@golang.org>
src/archive/zip/writer.go
src/archive/zip/writer_test.go

index c2e04163cf5a8cbe15c5e1b6a8ff185bbe0edb58..87ac694a4e673b17f9ded3a8cdd28997467be30b 100644 (file)
@@ -29,29 +29,20 @@ type header struct {
        offset uint64
 }
 
-// WriterOptions contains configuration options for a zip.Writer.
-type WriterOptions struct {
-       // Offset modifies the initial zip offset.
-       // This is useful when the zip is appended to other data such as a binary executable.
-       Offset int64
-}
-
 // NewWriter returns a new Writer writing a zip file to w.
 func NewWriter(w io.Writer) *Writer {
-       return NewWriterWithOptions(w, nil)
+       return &Writer{cw: &countWriter{w: bufio.NewWriter(w)}}
 }
 
-// NewWriterWithOptions returns a new Writer writing a zip file to w and uses the given options.
-func NewWriterWithOptions(w io.Writer, options *WriterOptions) *Writer {
-       writer := &Writer{
-               cw: &countWriter{
-                       w: bufio.NewWriter(w),
-               },
-       }
-       if options != nil {
-               writer.cw.count = options.Offset
+// SetOffset sets the offset of the beginning of the zip data within the
+// underlying writer. It should be used when the zip data is appended to an
+// existing file, such as a binary executable.
+// It must be called before any data is written.
+func (w *Writer) SetOffset(n int64) {
+       if w.cw.count != 0 {
+               panic("zip: SetOffset called after data was written")
        }
-       return writer
+       w.cw.count = n
 }
 
 // Flush flushes any buffered data to the underlying writer.
index 8be86408d80c3c721cf7bc7f35762c6f5e4dd4c5..01b63f2358d41c258baf08b1f5206b11f41dfcc9 100644 (file)
@@ -87,7 +87,7 @@ func TestWriter(t *testing.T) {
        }
 }
 
-func TestWriterOffsetOption(t *testing.T) {
+func TestWriterOffset(t *testing.T) {
        largeData := make([]byte, 1<<17)
        for i := range largeData {
                largeData[i] = byte(rand.Int())
@@ -101,7 +101,8 @@ func TestWriterOffsetOption(t *testing.T) {
        buf := new(bytes.Buffer)
        existingData := []byte{1, 2, 3, 1, 2, 3, 1, 2, 3}
        n, _ := buf.Write(existingData)
-       w := NewWriterWithOptions(buf, &WriterOptions{Offset: int64(n)})
+       w := NewWriter(buf)
+       w.SetOffset(int64(n))
 
        for _, wt := range writeTests {
                testCreate(t, w, &wt)