mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-09 01:30:25 +08:00
optimize: unpack
This commit is contained in:
51
unpack.go
51
unpack.go
@@ -5,7 +5,6 @@ import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
)
|
||||
|
||||
// This receives a zip file (word documents are a zip with multiple xml inside)
|
||||
@@ -16,63 +15,51 @@ func unpack(zipReader *zip.Reader) (docx *Docx, err error) {
|
||||
docx = new(Docx)
|
||||
for _, f := range zipReader.File {
|
||||
if f.Name == "word/_rels/document.xml.rels" {
|
||||
err = processRelations(f, &docx.DocRelation)
|
||||
err = docx.parseDocRelation(f)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if f.Name == "word/document.xml" {
|
||||
err = processDoc(f, &docx.Document)
|
||||
err = docx.parseDocument(f)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
//TODO: find last rId
|
||||
docx.buf = bytes.NewBuffer(make([]byte, 0, 1024*1024*4))
|
||||
return
|
||||
}
|
||||
|
||||
// Processes one of the relevant files, the one with the actual document
|
||||
func processDoc(file *zip.File, doc *Document) error {
|
||||
filebytes, err := readZipFile(file)
|
||||
// parseDocument processes one of the relevant files, the one with the actual document
|
||||
func (f *Docx) parseDocument(file *zip.File) error {
|
||||
zf, err := file.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer zf.Close()
|
||||
|
||||
doc.XMLW = XMLNS_W
|
||||
doc.XMLR = XMLNS_R
|
||||
doc.XMLWP = XMLNS_WP
|
||||
doc.XMLName.Space = XMLNS_W
|
||||
doc.XMLName.Local = "document"
|
||||
err = xml.Unmarshal(filebytes, doc)
|
||||
f.Document.XMLW = XMLNS_W
|
||||
f.Document.XMLR = XMLNS_R
|
||||
f.Document.XMLWP = XMLNS_WP
|
||||
f.Document.XMLName.Space = XMLNS_W
|
||||
f.Document.XMLName.Local = "document"
|
||||
err = xml.NewDecoder(zf).Decode(&f.Document)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Processes one of the relevant files, the one with the relationships
|
||||
func processRelations(file *zip.File, rels *Relationships) error {
|
||||
filebytes, err := readZipFile(file)
|
||||
// parseDocRelation processes one of the relevant files, the one with the relationships
|
||||
func (f *Docx) parseDocRelation(file *zip.File) error {
|
||||
zf, err := file.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer zf.Close()
|
||||
|
||||
rels.Xmlns = XMLNS_R
|
||||
err = xml.Unmarshal(filebytes, rels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return 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 io.ReadAll(f)
|
||||
f.DocRelation.Xmlns = XMLNS_R
|
||||
//TODO: find last rId
|
||||
return xml.NewDecoder(zf).Decode(&f.DocRelation)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user