-
Notifications
You must be signed in to change notification settings - Fork 9
/
unpack.go
88 lines (80 loc) · 2.12 KB
/
unpack.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
package docxlib
// This contains internal functions needed to unpack (read) a zip file
import (
"archive/zip"
"encoding/xml"
"io/ioutil"
"github.com/golang/glog"
)
// This receives a zip file (word documents are a zip with multiple xml inside)
// and parses the files that are relevant for us:
// 1.-Document
// 2.-Relationships
func unpack(zipReader *zip.Reader) (docx *DocxLib, err error) {
var doc *Document
var relations *Relationships
for _, f := range zipReader.File {
if f.Name == "word/_rels/document.xml.rels" {
relations, err = processRelations(f)
if err != nil {
return nil, err
}
}
if f.Name == "word/document.xml" {
doc, err = processDoc(f)
if err != nil {
return nil, err
}
}
}
docx = &DocxLib{
Document: *doc,
DocRelation: *relations,
}
return docx, nil
}
// Processes one of the relevant files, the one with the actual document
func processDoc(file *zip.File) (*Document, error) {
filebytes, err := readZipFile(file)
if err != nil {
glog.Errorln("Error reading from internal zip file")
return nil, err
}
glog.V(0).Infoln("Doc:", string(filebytes))
doc := Document{
XMLW: XMLNS_W,
XMLR: XMLNS_R,
XMLName: xml.Name{Space: XMLNS_W, Local: "document"}}
err = xml.Unmarshal(filebytes, &doc)
if err != nil {
glog.Errorln("Error unmarshalling doc", string(filebytes))
return nil, err
}
glog.V(0).Infoln("Paragraph", doc.Body.Paragraphs)
return &doc, nil
}
// Processes one of the relevant files, the one with the relationships
func processRelations(file *zip.File) (*Relationships, error) {
filebytes, err := readZipFile(file)
if err != nil {
glog.Errorln("Error reading from internal zip file")
return nil, err
}
glog.V(0).Infoln("Relations:", string(filebytes))
rels := Relationships{Xmlns: XMLNS_R}
err = xml.Unmarshal(filebytes, &rels)
if err != nil {
glog.Errorln("Error unmarshalling relationships")
return nil, err
}
return &rels, nil
}
// From a zip file structure, we return a byte array
func readZipFile(zf *zip.File) ([]byte, error) {
f, err := zf.Open()
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}