mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-05 15:50:24 +08:00
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package docxlib
|
|
|
|
import (
|
|
"archive/zip"
|
|
"encoding/xml"
|
|
"fmt"
|
|
)
|
|
|
|
// 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 {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
out = xml.Header + string(body)
|
|
return
|
|
}
|