mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-11 19:10:24 +08:00
feat: pack use io.Copy
This commit is contained in:
41
pack.go
41
pack.go
@@ -2,8 +2,9 @@ package docxlib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bytes"
|
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This receives a zip file writer (word documents are a zip with multiple xml inside)
|
// This receives a zip file writer (word documents are a zip with multiple xml inside)
|
||||||
@@ -18,30 +19,24 @@ func (f *Docx) pack(zipWriter *zip.Writer) (err error) {
|
|||||||
"word/styles.xml",
|
"word/styles.xml",
|
||||||
"[Content_Types].xml",
|
"[Content_Types].xml",
|
||||||
}
|
}
|
||||||
files := make(map[string][]byte, 64)
|
files := make(map[string]io.Reader, 64)
|
||||||
|
|
||||||
for _, name := range fileslst {
|
for _, name := range fileslst {
|
||||||
files[name], err = TEMP_XML_FS.ReadFile("xml/" + name)
|
files[name], err = TEMP_XML_FS.Open("xml/" + name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
files["word/_rels/document.xml.rels"], err = marshal(f.DocRelation)
|
files["word/_rels/document.xml.rels"] = marshaller{data: f.DocRelation}
|
||||||
if err != nil {
|
files["word/document.xml"] = marshaller{data: f.Document}
|
||||||
return
|
|
||||||
}
|
|
||||||
files["word/document.xml"], err = marshal(f.Document)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for path, data := range files {
|
for path, r := range files {
|
||||||
w, err := zipWriter.Create(path)
|
w, err := zipWriter.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = w.Write(data)
|
_, err = io.Copy(w, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -50,13 +45,23 @@ func (f *Docx) pack(zipWriter *zip.Writer) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func marshal(data interface{}) (out []byte, err error) {
|
type marshaller struct {
|
||||||
buf := bytes.NewBuffer(make([]byte, 0, 1024))
|
data interface{}
|
||||||
buf.WriteString(xml.Header)
|
io.Reader
|
||||||
err = xml.NewEncoder(buf).Encode(data)
|
io.WriterTo
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read is fake and is to trigger io.WriterTo
|
||||||
|
func (m marshaller) Read(p []byte) (n int, err error) {
|
||||||
|
return 0, os.ErrInvalid
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteTo n is always 0 for we don't care that value
|
||||||
|
func (m marshaller) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
|
_, err = io.WriteString(w, xml.Header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
out = buf.Bytes()
|
err = xml.NewEncoder(w).Encode(m.data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -575,10 +575,13 @@ func TestMarshalDrawingStructure(t *testing.T) {
|
|||||||
nextPara := w.AddParagraph()
|
nextPara := w.AddParagraph()
|
||||||
nextPara.AddLink("google", `http://google.com`)
|
nextPara.AddLink("google", `http://google.com`)
|
||||||
|
|
||||||
doc, err := marshal(w.Document)
|
f, err := os.Create("test.xml")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_, err = marshaller{data: w.Document}.WriteTo(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
os.WriteFile("test.xml", doc, 0644)
|
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user