forked from sselph/scraper
/
7zip.go
44 lines (39 loc) · 769 Bytes
/
7zip.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
package hash
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/kjk/lzmadec"
)
var has7z bool
func init() {
if runtime.GOOS == "windows" {
os.Setenv("PATH", fmt.Sprintf("C:\\Program Files\\7-zip;%s", os.Getenv("PATH")))
}
_, err := exec.LookPath("7z")
has7z = err == nil
}
func decode7Zip(f string) (io.ReadCloser, error) {
r, err := lzmadec.NewArchive(f)
if err != nil {
return nil, err
}
for _, e := range r.Entries {
ext := filepath.Ext(e.Path)
if decoder, ok := getDecoder(ext); ok {
rf, err := r.GetFileReader(e.Path)
if err != nil {
continue
}
rom, err := decoder(rf, e.Size)
if err != nil {
continue
}
return rom, nil
}
}
return nil, fmt.Errorf("No valid roms found in 7zip.")
}