This repository has been archived by the owner on Jan 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor.go
90 lines (79 loc) · 1.94 KB
/
extractor.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/vazrupe/go-acb/acb"
)
func main() {
defaultDir := ""
saveDir := flag.String("save", defaultDir, "extract dir")
force := flag.Bool("f", false, "if an existing destination file cannot be opened, remove it and try again")
flag.Parse()
files := flag.Args()
var saveRoot string
for _, filename := range files {
f, err := acb.LoadCriAcbFile(filename)
if err != nil {
fmt.Printf("Error: %s Open Failed (%s)\n", filename, err)
}
name := filepath.Base(filename)
if (*saveDir) == defaultDir {
ext := filepath.Ext(filename)
saveRoot = filename[:len(filename)-len(ext)]
} else {
ext := filepath.Ext(filename)
dirName := name[:len(name)-len(ext)]
saveRoot = filepath.Join(*saveDir, dirName)
}
if _, err := os.Stat(saveRoot); err == nil {
if !*force {
fmt.Printf("Exists: directory `%s`. skip\n", saveRoot)
continue
}
RemoveDir(saveRoot)
}
i := SaveAcb(saveRoot, f)
fmt.Printf("Extract: %s -> %s (%d files)\n", name, saveRoot, i)
}
}
// RemoveDir is remove all files on dir
func RemoveDir(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
return err
}
}
return nil
}
// SaveAcb is extract AcbFile on target dir
func SaveAcb(savedir string, a *acb.CriAcbFile) int {
i := 0
for _, cue := range a.Cue {
file, ok := a.InternalAwb.Files[uint16(cue.CueID)]
if !ok {
continue
}
savename := fmt.Sprintf("%s%s", cue.CueName, cue.GetFileExtension())
savePath := filepath.Join(savedir, savename)
os.MkdirAll(savedir, os.ModeDir)
f, err := os.Create(savePath)
if err == nil {
i++
}
defer f.Close()
f.Write(file.Data)
}
return i
}