forked from kapmahc/epub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopen.go
64 lines (54 loc) · 1.23 KB
/
open.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
package epub
import "archive/zip"
//Open open a epub file
func Open(fn string) (*Book, error) {
fd, err := zip.OpenReader(fn)
if err != nil {
return nil, err
}
bk := Book{fd: fd}
mt, err := bk.readBytes("mimetype")
if err == nil {
bk.Mimetype = string(mt)
err = bk.readXML("META-INF/container.xml", &bk.Container)
}
if err == nil {
err = bk.readXML(bk.Container.Rootfile.Path, &bk.Opf)
}
if err == nil {
err = bk.readXML("META-INF/encryption.xml", &bk.Encryption)
}
for _, mf := range bk.Opf.Manifest {
if mf.ID == bk.Opf.Spine.Toc {
err = bk.readXML(bk.filename(mf.Href), &bk.Ncx)
break
}
}
if err != nil {
fd.Close()
return nil, err
}
return &bk, nil
}
//OpenDir open a opf file
func OpenDir(fn string) (*Book, error) {
bk := Book{directory: fn}
mt, err := bk.readBytes("mimetype")
if err == nil {
bk.Mimetype = string(mt)
err = bk.readXML("META-INF/container.xml", &bk.Container)
}
if err == nil {
err = bk.readXML(bk.Container.Rootfile.Path, &bk.Opf)
}
if err == nil {
err = bk.readXML("META-INF/encryption.xml", &bk.Encryption)
}
for _, mf := range bk.Opf.Manifest {
if mf.ID == bk.Opf.Spine.Toc {
err = bk.readXML(bk.filename(mf.Href), &bk.Ncx)
break
}
}
return &bk, nil
}