1
0
mirror of https://github.com/golang/go synced 2024-11-22 03:54:39 -07:00

archive/zip: hide Write method from *Writer type

This was an implementation detail that snuck into the public interface.
*Writer.Create gives you an io.Writer, the *Writer itself was never
meant to be written to.

R=golang-dev, dsymonds, r
CC=golang-dev
https://golang.org/cl/5654076
This commit is contained in:
Andrew Gerrand 2012-02-14 10:47:48 +11:00
parent 2d53d227f6
commit 04868b28ac
3 changed files with 61 additions and 37 deletions

View File

@ -855,6 +855,18 @@ few programs beyond the need to run <code>go fix</code>.
This category includes packages that are new in Go 1. This category includes packages that are new in Go 1.
</p> </p>
<h3 id="archive_zip">The archive/zip package</h3>
<p>
In Go 1, <a href="/pkg/archive/zip/#Writer"><code>*zip.Writer</code></a> no
longer has a <code>Write</code> method. Its presence was a mistake.
</p>
<p>
<i>Updating:</i> What little code is affected will be caught by the compiler
and must be updated by hand. Such code is almost certainly incorrect.
</p>
<h3 id="crypto_aes_des">The crypto/aes and crypto/des packages</h3> <h3 id="crypto_aes_des">The crypto/aes and crypto/des packages</h3>
<p> <p>

View File

@ -759,6 +759,18 @@ few programs beyond the need to run <code>go fix</code>.
This category includes packages that are new in Go 1. This category includes packages that are new in Go 1.
</p> </p>
<h3 id="archive_zip">The archive/zip package</h3>
<p>
In Go 1, <a href="/pkg/archive/zip/#Writer"><code>*zip.Writer</code></a> no
longer has a <code>Write</code> method. Its presence was a mistake.
</p>
<p>
<i>Updating:</i> What little code is affected will be caught by the compiler
and must be updated by hand. Such code is almost certainly incorrect.
</p>
<h3 id="crypto_aes_des">The crypto/aes and crypto/des packages</h3> <h3 id="crypto_aes_des">The crypto/aes and crypto/des packages</h3>
<p> <p>

View File

@ -19,7 +19,7 @@ import (
// Writer implements a zip file writer. // Writer implements a zip file writer.
type Writer struct { type Writer struct {
countWriter cw *countWriter
dir []*header dir []*header
last *fileWriter last *fileWriter
closed bool closed bool
@ -32,7 +32,7 @@ type header struct {
// NewWriter returns a new Writer writing a zip file to w. // NewWriter returns a new Writer writing a zip file to w.
func NewWriter(w io.Writer) *Writer { func NewWriter(w io.Writer) *Writer {
return &Writer{countWriter: countWriter{w: bufio.NewWriter(w)}} return &Writer{cw: &countWriter{w: bufio.NewWriter(w)}}
} }
// Close finishes writing the zip file by writing the central directory. // Close finishes writing the zip file by writing the central directory.
@ -52,42 +52,42 @@ func (w *Writer) Close() (err error) {
defer recoverError(&err) defer recoverError(&err)
// write central directory // write central directory
start := w.count start := w.cw.count
for _, h := range w.dir { for _, h := range w.dir {
write(w, uint32(directoryHeaderSignature)) write(w.cw, uint32(directoryHeaderSignature))
write(w, h.CreatorVersion) write(w.cw, h.CreatorVersion)
write(w, h.ReaderVersion) write(w.cw, h.ReaderVersion)
write(w, h.Flags) write(w.cw, h.Flags)
write(w, h.Method) write(w.cw, h.Method)
write(w, h.ModifiedTime) write(w.cw, h.ModifiedTime)
write(w, h.ModifiedDate) write(w.cw, h.ModifiedDate)
write(w, h.CRC32) write(w.cw, h.CRC32)
write(w, h.CompressedSize) write(w.cw, h.CompressedSize)
write(w, h.UncompressedSize) write(w.cw, h.UncompressedSize)
write(w, uint16(len(h.Name))) write(w.cw, uint16(len(h.Name)))
write(w, uint16(len(h.Extra))) write(w.cw, uint16(len(h.Extra)))
write(w, uint16(len(h.Comment))) write(w.cw, uint16(len(h.Comment)))
write(w, uint16(0)) // disk number start write(w.cw, uint16(0)) // disk number start
write(w, uint16(0)) // internal file attributes write(w.cw, uint16(0)) // internal file attributes
write(w, h.ExternalAttrs) write(w.cw, h.ExternalAttrs)
write(w, h.offset) write(w.cw, h.offset)
writeBytes(w, []byte(h.Name)) writeBytes(w.cw, []byte(h.Name))
writeBytes(w, h.Extra) writeBytes(w.cw, h.Extra)
writeBytes(w, []byte(h.Comment)) writeBytes(w.cw, []byte(h.Comment))
} }
end := w.count end := w.cw.count
// write end record // write end record
write(w, uint32(directoryEndSignature)) write(w.cw, uint32(directoryEndSignature))
write(w, uint16(0)) // disk number write(w.cw, uint16(0)) // disk number
write(w, uint16(0)) // disk number where directory starts write(w.cw, uint16(0)) // disk number where directory starts
write(w, uint16(len(w.dir))) // number of entries this disk write(w.cw, uint16(len(w.dir))) // number of entries this disk
write(w, uint16(len(w.dir))) // number of entries total write(w.cw, uint16(len(w.dir))) // number of entries total
write(w, uint32(end-start)) // size of directory write(w.cw, uint32(end-start)) // size of directory
write(w, uint32(start)) // start of directory write(w.cw, uint32(start)) // start of directory
write(w, uint16(0)) // size of comment write(w.cw, uint16(0)) // size of comment
return w.w.(*bufio.Writer).Flush() return w.cw.w.(*bufio.Writer).Flush()
} }
// Create adds a file to the zip file using the provided name. // Create adds a file to the zip file using the provided name.
@ -119,8 +119,8 @@ func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
fh.ReaderVersion = 0x14 fh.ReaderVersion = 0x14
fw := &fileWriter{ fw := &fileWriter{
zipw: w, zipw: w.cw,
compCount: &countWriter{w: w}, compCount: &countWriter{w: w.cw},
crc32: crc32.NewIEEE(), crc32: crc32.NewIEEE(),
} }
switch fh.Method { switch fh.Method {
@ -139,12 +139,12 @@ func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
h := &header{ h := &header{
FileHeader: fh, FileHeader: fh,
offset: uint32(w.count), offset: uint32(w.cw.count),
} }
w.dir = append(w.dir, h) w.dir = append(w.dir, h)
fw.header = h fw.header = h
if err := writeHeader(w, fh); err != nil { if err := writeHeader(w.cw, fh); err != nil {
return nil, err return nil, err
} }