forked from sselph/scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip.go
50 lines (45 loc) · 857 Bytes
/
zip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package rom
import (
"archive/zip"
"fmt"
"io"
"path"
"strings"
)
type ZipReader struct {
f *zip.ReadCloser
rom io.ReadCloser
}
func (r ZipReader) Read(p []byte) (int, error) {
n, err := r.rom.Read(p)
return n, err
}
func (r ZipReader) Close() error {
r.rom.Close()
return r.f.Close()
}
func decodeZip(f string) (io.ReadCloser, error) {
r, err := zip.OpenReader(f)
if err != nil {
return nil, err
}
var zr ZipReader
for _, zf := range r.File {
ext := strings.ToLower(path.Ext(zf.FileHeader.Name))
if KnownExt(ext) {
rf, err := zf.Open()
if err != nil {
continue
}
rs := zf.FileHeader.UncompressedSize64
decoder := formats[ext]
rom, err := decoder(rf, int64(rs))
if err != nil {
continue
}
zr = ZipReader{r, rom}
return zr, nil
}
}
return nil, fmt.Errorf("No valid roms found in zip.")
}