1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-10 10:23:19 +08:00

First commit

This commit is contained in:
Gonzalo Fernandez-Victorio
2021-04-23 16:58:31 +01:00
parent bd1f5d3e9b
commit eae5f90385
16 changed files with 957 additions and 0 deletions

77
unpack.go Normal file
View File

@@ -0,0 +1,77 @@
package docxlib
import (
"archive/zip"
"encoding/xml"
"fmt"
"io/ioutil"
)
func unpack(zipReader *zip.Reader) (docx *Docx, err error) {
var doc *Document
var relations *Relationships
for _, f := range zipReader.File {
if f.Name == "word/_rels/document.xml.rels" {
relations, err = processRelations(f)
if err != nil {
return nil, err
}
}
if f.Name == "word/document.xml" {
doc, err = processDoc(f)
if err != nil {
return nil, err
}
}
}
docx = &Docx{
Document: *doc,
DocRelation: *relations,
}
return docx, nil
}
func processDoc(file *zip.File) (*Document, error) {
filebytes, err := readZipFile(file)
if err != nil {
fmt.Println("Error reading from internal zip file")
return nil, err
}
doc := Document{
XMLW: XMLNS_W,
XMLR: XMLNS_R,
XMLName: xml.Name{Space: XMLNS_W, Local: "document"}}
err = xml.Unmarshal(filebytes, &doc)
//r := bytes.NewReader(filebytes)
//err = decode(r)
if err != nil {
fmt.Println("Error unmarshalling doc")
fmt.Println(string(filebytes))
return nil, err
}
return &doc, nil
}
func processRelations(file *zip.File) (*Relationships, error) {
filebytes, err := readZipFile(file)
if err != nil {
fmt.Println("Error reading from internal zip file")
return nil, err
}
rels := Relationships{Xmlns: "none"}
err = xml.Unmarshal(filebytes, &rels)
if err != nil {
fmt.Println("Error unmarshalling relationships")
return nil, err
}
return &rels, nil
}
func readZipFile(zf *zip.File) ([]byte, error) {
f, err := zf.Open()
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}