-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add adapted appendedzip implementation
This is from [Spyre](https://github.com/spyre-project/spyre), with crypto zip support removed and a function renamed for consistency
- Loading branch information
Showing
10 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package appendedzip | ||
|
||
import ( | ||
"archive/zip" | ||
"bytes" | ||
"errors" | ||
"io" | ||
"os" | ||
) | ||
|
||
func OpenFile(file string) (*zip.Reader, error) { | ||
f, err := os.Open(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fi, err := f.Stat() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewReader(f, fi.Size()) | ||
} | ||
|
||
// OpenReader searches for ZIP beginning-of-file signatures ('P' 'K' | ||
// 03 04) in r and tries to read the file starting at that offset | ||
// using an encryption-enabled archive/zip, returning a *zip.Reader | ||
// for the first valid entry, or an error. | ||
func NewReader(r io.ReaderAt, size int64) (*zip.Reader, error) { | ||
const BUFSIZE = 4096 | ||
var buf [BUFSIZE + 4]byte | ||
for i := int64(0); (i-1)*BUFSIZE < size; i++ { | ||
len, err := r.ReadAt(buf[:], i*BUFSIZE) | ||
if err != nil && err != io.EOF { | ||
break | ||
} | ||
|
||
n := 0 | ||
for { | ||
m := bytes.Index(buf[n:len], []byte("PK\x03\x04")) | ||
if m == -1 { | ||
break | ||
} | ||
off := i*BUFSIZE + int64(n+m) | ||
ssize := size - int64(off) | ||
sr := io.NewSectionReader(r, int64(off), ssize) | ||
if zr, ze := zip.NewReader(sr, ssize+1); ze == nil { | ||
return zr, nil | ||
} | ||
n += m + 1 | ||
} | ||
if err == io.EOF { | ||
break | ||
} | ||
} | ||
return nil, errors.New("No zip file found") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package appendedzip | ||
|
||
import ( | ||
"archive/zip" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestAppendedZip(t *testing.T) { | ||
for _, sample := range []string{ | ||
"data512", | ||
"data8192", | ||
"data8190", | ||
"doublezip", | ||
"fakeheader8192", | ||
"fakeheader512", | ||
"regular", | ||
} { | ||
t.Log("Reading " + sample + " ...") | ||
f, err := os.Open("testdata/" + sample) | ||
if _, err := zip.OpenReader("testdata/" + sample); err == nil { | ||
t.Logf("Note: %s can be opened by archive/zip", sample) | ||
} | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
fi, err := f.Stat() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
zr, err := NewReader(f, fi.Size()) | ||
if err != nil { | ||
t.Error(err) | ||
} else { | ||
t.Log("found zip in " + sample) | ||
t.Logf("%+v", zr) | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters