1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-05 07:40:24 +08:00
Files
go-docx/pack.go
Gonzalo Fernandez-Victorio 54bd3d2d4a Logging
2021-05-11 12:40:58 +01:00

56 lines
1.2 KiB
Go

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
}