archive/zip: if non-zero base offset fails, fall back to zero

This permits us to read files that earlier Go releases could read.
It is also compatible with other zip programs.

Change-Id: I7e2999f1073c4db5ba3f51f92681e0b149d55b3e
Reviewed-on: https://go-review.googlesource.com/c/go/+/408734
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Ian Lance Taylor 2022-05-25 14:12:29 -07:00 committed by Gopher Robot
parent 46ab7a5c4f
commit f70b93a6e9
3 changed files with 32 additions and 0 deletions

View file

@ -123,6 +123,20 @@ func (z *Reader) init(r io.ReaderAt, size int64) error {
for {
f := &File{zip: z, zipr: r}
err = readDirectoryHeader(f, buf)
// For compatibility with other zip programs,
// if we have a non-zero base offset and can't read
// the first directory header, try again with a zero
// base offset.
if err == ErrFormat && z.baseOffset != 0 && len(z.File) == 0 {
z.baseOffset = 0
if _, err = rs.Seek(int64(end.directoryOffset), io.SeekStart); err != nil {
return err
}
buf = bufio.NewReader(rs)
continue
}
if err == ErrFormat || err == io.ErrUnexpectedEOF {
break
}

View file

@ -108,6 +108,24 @@ var tests = []ZipTest{
},
},
},
{
Name: "test-baddirsz.zip",
Comment: "This is a zipfile comment.",
File: []ZipTestFile{
{
Name: "test.txt",
Content: []byte("This is a test text file.\n"),
Modified: time.Date(2010, 9, 5, 12, 12, 1, 0, timeZone(+10*time.Hour)),
Mode: 0644,
},
{
Name: "gophercolor16x16.png",
File: "gophercolor16x16.png",
Modified: time.Date(2010, 9, 5, 15, 52, 58, 0, timeZone(+10*time.Hour)),
Mode: 0644,
},
},
},
{
Name: "r.zip",
Source: returnRecursiveZip,

Binary file not shown.