1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
20 ErrFormat
= errors
.New("zip: not a valid zip file")
21 ErrAlgorithm
= errors
.New("zip: unsupported compression algorithm")
22 ErrChecksum
= errors
.New("zip: checksum error")
31 type ReadCloser
struct {
43 func (f
*File
) hasDataDescriptor() bool {
44 return f
.Flags
&0x8 != 0
47 // OpenReader will open the Zip file specified by name and return a ReadCloser.
48 func OpenReader(name
string) (*ReadCloser
, error
) {
49 f
, err
:= os
.Open(name
)
59 if err
:= r
.init(f
, fi
.Size()); err
!= nil {
67 // NewReader returns a new Reader reading from r, which is assumed to
68 // have the given size in bytes.
69 func NewReader(r io
.ReaderAt
, size
int64) (*Reader
, error
) {
71 if err
:= zr
.init(r
, size
); err
!= nil {
77 func (z
*Reader
) init(r io
.ReaderAt
, size
int64) error
{
78 end
, err
:= readDirectoryEnd(r
, size
)
83 z
.File
= make([]*File
, 0, end
.directoryRecords
)
84 z
.Comment
= end
.comment
85 rs
:= io
.NewSectionReader(r
, 0, size
)
86 if _
, err
= rs
.Seek(int64(end
.directoryOffset
), os
.SEEK_SET
); err
!= nil {
89 buf
:= bufio
.NewReader(rs
)
91 // The count of files inside a zip is truncated to fit in a uint16.
92 // Gloss over this by reading headers until we encounter
93 // a bad one, and then only report a ErrFormat or UnexpectedEOF if
94 // the file count modulo 65536 is incorrect.
96 f
:= &File
{zipr
: r
, zipsize
: size
}
97 err
= readDirectoryHeader(f
, buf
)
98 if err
== ErrFormat || err
== io
.ErrUnexpectedEOF
{
104 z
.File
= append(z
.File
, f
)
106 if uint16(len(z
.File
)) != uint16(end
.directoryRecords
) { // only compare 16 bits here
107 // Return the readDirectoryHeader error if we read
108 // the wrong number of directory entries.
114 // Close closes the Zip file, rendering it unusable for I/O.
115 func (rc
*ReadCloser
) Close() error
{
119 // Open returns a ReadCloser that provides access to the File's contents.
120 // Multiple files may be read concurrently.
121 func (f
*File
) Open() (rc io
.ReadCloser
, err error
) {
122 bodyOffset
, err
:= f
.findBodyOffset()
126 size
:= int64(f
.CompressedSize64
)
127 r
:= io
.NewSectionReader(f
.zipr
, f
.headerOffset
+bodyOffset
, size
)
129 case Store
: // (no compression)
130 rc
= ioutil
.NopCloser(r
)
132 rc
= flate
.NewReader(r
)
138 if f
.hasDataDescriptor() {
139 desr
= io
.NewSectionReader(f
.zipr
, f
.headerOffset
+bodyOffset
+size
, dataDescriptorLen
)
141 rc
= &checksumReader
{rc
, crc32
.NewIEEE(), f
, desr
, nil}
145 type checksumReader
struct {
149 desr io
.Reader
// if non-nil, where to read the data descriptor
150 err error
// sticky error
153 func (r
*checksumReader
) Read(b
[]byte) (n
int, err error
) {
157 n
, err
= r
.rc
.Read(b
)
164 if err1
:= readDataDescriptor(r
.desr
, r
.f
); err1
!= nil {
166 } else if r
.hash
.Sum32() != r
.f
.CRC32
{
170 // If there's not a data descriptor, we still compare
171 // the CRC32 of what we've read against the file header
172 // or TOC's CRC32, if it seems like it was set.
173 if r
.f
.CRC32
!= 0 && r
.hash
.Sum32() != r
.f
.CRC32
{
182 func (r
*checksumReader
) Close() error
{ return r
.rc
.Close() }
184 // findBodyOffset does the minimum work to verify the file has a header
185 // and returns the file body offset.
186 func (f
*File
) findBodyOffset() (int64, error
) {
187 r
:= io
.NewSectionReader(f
.zipr
, f
.headerOffset
, f
.zipsize
-f
.headerOffset
)
188 var buf
[fileHeaderLen
]byte
189 if _
, err
:= io
.ReadFull(r
, buf
[:]); err
!= nil {
193 if sig
:= b
.uint32(); sig
!= fileHeaderSignature
{
196 b
= b
[22:] // skip over most of the header
197 filenameLen
:= int(b
.uint16())
198 extraLen
:= int(b
.uint16())
199 return int64(fileHeaderLen
+ filenameLen
+ extraLen
), nil
202 // readDirectoryHeader attempts to read a directory header from r.
203 // It returns io.ErrUnexpectedEOF if it cannot read a complete header,
204 // and ErrFormat if it doesn't find a valid header signature.
205 func readDirectoryHeader(f
*File
, r io
.Reader
) error
{
206 var buf
[directoryHeaderLen
]byte
207 if _
, err
:= io
.ReadFull(r
, buf
[:]); err
!= nil {
211 if sig
:= b
.uint32(); sig
!= directoryHeaderSignature
{
214 f
.CreatorVersion
= b
.uint16()
215 f
.ReaderVersion
= b
.uint16()
217 f
.Method
= b
.uint16()
218 f
.ModifiedTime
= b
.uint16()
219 f
.ModifiedDate
= b
.uint16()
221 f
.CompressedSize
= b
.uint32()
222 f
.UncompressedSize
= b
.uint32()
223 f
.CompressedSize64
= uint64(f
.CompressedSize
)
224 f
.UncompressedSize64
= uint64(f
.UncompressedSize
)
225 filenameLen
:= int(b
.uint16())
226 extraLen
:= int(b
.uint16())
227 commentLen
:= int(b
.uint16())
228 b
= b
[4:] // skipped start disk number and internal attributes (2x uint16)
229 f
.ExternalAttrs
= b
.uint32()
230 f
.headerOffset
= int64(b
.uint32())
231 d
:= make([]byte, filenameLen
+extraLen
+commentLen
)
232 if _
, err
:= io
.ReadFull(r
, d
); err
!= nil {
235 f
.Name
= string(d
[:filenameLen
])
236 f
.Extra
= d
[filenameLen
: filenameLen
+extraLen
]
237 f
.Comment
= string(d
[filenameLen
+extraLen
:])
239 if len(f
.Extra
) > 0 {
240 b
:= readBuf(f
.Extra
)
241 for len(b
) >= 4 { // need at least tag and size
244 if int(size
) > len(b
) {
247 if tag
== zip64ExtraId
{
248 // update directory values from the zip64 extra block
251 f
.UncompressedSize64
= eb
.uint64()
254 f
.CompressedSize64
= eb
.uint64()
257 f
.headerOffset
= int64(eb
.uint64())
262 // Should have consumed the whole header.
270 func readDataDescriptor(r io
.Reader
, f
*File
) error
{
271 var buf
[dataDescriptorLen
]byte
273 // The spec says: "Although not originally assigned a
274 // signature, the value 0x08074b50 has commonly been adopted
275 // as a signature value for the data descriptor record.
276 // Implementers should be aware that ZIP files may be
277 // encountered with or without this signature marking data
278 // descriptors and should account for either case when reading
279 // ZIP files to ensure compatibility."
281 // dataDescriptorLen includes the size of the signature but
282 // first read just those 4 bytes to see if it exists.
283 if _
, err
:= io
.ReadFull(r
, buf
[:4]); err
!= nil {
287 maybeSig
:= readBuf(buf
[:4])
288 if maybeSig
.uint32() != dataDescriptorSignature
{
289 // No data descriptor signature. Keep these four
293 if _
, err
:= io
.ReadFull(r
, buf
[off
:12]); err
!= nil {
296 b
:= readBuf(buf
[:12])
297 if b
.uint32() != f
.CRC32
{
301 // The two sizes that follow here can be either 32 bits or 64 bits
302 // but the spec is not very clear on this and different
303 // interpretations has been made causing incompatibilities. We
304 // already have the sizes from the central directory so we can
305 // just ignore these.
310 func readDirectoryEnd(r io
.ReaderAt
, size
int64) (dir
*directoryEnd
, err error
) {
311 // look for directoryEndSignature in the last 1k, then in the last 65k
313 var directoryEndOffset
int64
314 for i
, bLen
:= range []int64{1024, 65 * 1024} {
318 buf
= make([]byte, int(bLen
))
319 if _
, err
:= r
.ReadAt(buf
, size
-bLen
); err
!= nil && err
!= io
.EOF
{
322 if p
:= findSignatureInBlock(buf
); p
>= 0 {
324 directoryEndOffset
= size
- bLen
+ int64(p
)
327 if i
== 1 || bLen
== size
{
328 return nil, ErrFormat
332 // read header into struct
333 b
:= readBuf(buf
[4:]) // skip signature
335 diskNbr
: uint32(b
.uint16()),
336 dirDiskNbr
: uint32(b
.uint16()),
337 dirRecordsThisDisk
: uint64(b
.uint16()),
338 directoryRecords
: uint64(b
.uint16()),
339 directorySize
: uint64(b
.uint32()),
340 directoryOffset
: uint64(b
.uint32()),
341 commentLen
: b
.uint16(),
343 l
:= int(d
.commentLen
)
345 return nil, errors
.New("zip: invalid comment length")
347 d
.comment
= string(b
[:l
])
349 p
, err
:= findDirectory64End(r
, directoryEndOffset
)
350 if err
== nil && p
>= 0 {
351 err
= readDirectory64End(r
, p
, d
)
357 // Make sure directoryOffset points to somewhere in our file.
358 if o
:= int64(d
.directoryOffset
); o
< 0 || o
>= size
{
359 return nil, ErrFormat
364 // findDirectory64End tries to read the zip64 locator just before the
365 // directory end and returns the offset of the zip64 directory end if
367 func findDirectory64End(r io
.ReaderAt
, directoryEndOffset
int64) (int64, error
) {
368 locOffset
:= directoryEndOffset
- directory64LocLen
370 return -1, nil // no need to look for a header outside the file
372 buf
:= make([]byte, directory64LocLen
)
373 if _
, err
:= r
.ReadAt(buf
, locOffset
); err
!= nil {
377 if sig
:= b
.uint32(); sig
!= directory64LocSignature
{
380 b
= b
[4:] // skip number of the disk with the start of the zip64 end of central directory
381 p
:= b
.uint64() // relative offset of the zip64 end of central directory record
385 // readDirectory64End reads the zip64 directory end and updates the
386 // directory end with the zip64 directory end values.
387 func readDirectory64End(r io
.ReaderAt
, offset
int64, d
*directoryEnd
) (err error
) {
388 buf
:= make([]byte, directory64EndLen
)
389 if _
, err
:= r
.ReadAt(buf
, offset
); err
!= nil {
394 if sig
:= b
.uint32(); sig
!= directory64EndSignature
{
398 b
= b
[12:] // skip dir size, version and version needed (uint64 + 2x uint16)
399 d
.diskNbr
= b
.uint32() // number of this disk
400 d
.dirDiskNbr
= b
.uint32() // number of the disk with the start of the central directory
401 d
.dirRecordsThisDisk
= b
.uint64() // total number of entries in the central directory on this disk
402 d
.directoryRecords
= b
.uint64() // total number of entries in the central directory
403 d
.directorySize
= b
.uint64() // size of the central directory
404 d
.directoryOffset
= b
.uint64() // offset of start of central directory with respect to the starting disk number
409 func findSignatureInBlock(b
[]byte) int {
410 for i
:= len(b
) - directoryEndLen
; i
>= 0; i
-- {
411 // defined from directoryEndSignature in struct.go
412 if b
[i
] == 'P' && b
[i
+1] == 'K' && b
[i
+2] == 0x05 && b
[i
+3] == 0x06 {
413 // n is length of comment
414 n
:= int(b
[i
+directoryEndLen
-2]) |
int(b
[i
+directoryEndLen
-1])<<8
415 if n
+directoryEndLen
+i
<= len(b
) {
425 func (b
*readBuf
) uint16() uint16 {
426 v
:= binary
.LittleEndian
.Uint16(*b
)
431 func (b
*readBuf
) uint32() uint32 {
432 v
:= binary
.LittleEndian
.Uint32(*b
)
437 func (b
*readBuf
) uint64() uint64 {
438 v
:= binary
.LittleEndian
.Uint64(*b
)