-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpack.go
54 lines (44 loc) · 1009 Bytes
/
pack.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
package main
import (
"io/ioutil"
"os"
)
func packFiles(book *Epub, input string) error {
folder, e := OpenVirtualFolder(input)
if e != nil {
logger.Println("failed to open source folder/file.\n")
return e
}
walk := func(path string) error {
rc, e := folder.OpenFile(path)
if e != nil {
logger.Println("failed to open file: ", path)
return e
}
defer rc.Close()
data, e := ioutil.ReadAll(rc)
if e != nil {
logger.Println("failed reading file: ", path)
return e
}
book.AddFile(path, data)
return e
}
return folder.Walk(walk)
}
func RunPack() {
inpath, outpath := getArg(0, ""), getArg(1, "")
if len(inpath) == 0 || len(outpath) == 0 {
onCommandLineError()
}
book := NewEpub(false)
if packFiles(book, inpath) != nil {
os.Exit(1)
}
if book.Save(outpath, EPUB_VERSION_NONE) != nil {
logger.Fatalln("failed to create output file: ", outpath)
}
}
func init() {
AddCommandHandler("p", RunPack)
}