-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpack.go
55 lines (46 loc) · 1.22 KB
/
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
55
package docxlib
import (
"archive/zip"
"encoding/xml"
"github.com/golang/glog"
)
// This receives a zip file writer (word documents are a zip with multiple xml inside)
// and writes the relevant files. Some of them come from the empty_constants file,
// others from the actual in-memory structure
func (f *DocxLib) pack(zipWriter *zip.Writer) (err error) {
files := map[string]string{}
files["_rels/.rels"] = TEMP_REL
files["docProps/app.xml"] = TEMP_DOCPROPS_APP
files["docProps/core.xml"] = TEMP_DOCPROPS_CORE
files["word/theme/theme1.xml"] = TEMP_WORD_THEME_THEME
files["word/styles.xml"] = TEMP_WORD_STYLE
files["[Content_Types].xml"] = TEMP_CONTENT
files["word/_rels/document.xml.rels"], err = marshal(f.DocRelation)
if err != nil {
return err
}
files["word/document.xml"], err = marshal(f.Document)
if err != nil {
return err
}
for path, data := range files {
w, err := zipWriter.Create(path)
if err != nil {
return err
}
_, err = w.Write([]byte(data))
if err != nil {
return err
}
}
return
}
func marshal(data interface{}) (out string, err error) {
body, err := xml.Marshal(data)
if err != nil {
glog.Errorln("Error marshalling", err)
return
}
out = xml.Header + string(body)
return
}