1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-12 11:33:43 +08:00

feat: Docx implements io.WriterTo

This commit is contained in:
源文雨
2023-02-09 15:22:47 +08:00
parent f995e72acd
commit 56810d8288
5 changed files with 62 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ package docxlib
import (
"archive/zip"
"bytes"
"errors"
"io"
)
@@ -18,6 +19,11 @@ type Docx struct {
DocRelation Relationships
rId uintptr
buf *bytes.Buffer
isbufempty bool
io.Reader
io.WriterTo
}
// New generates a new empty docx file that we can manipulate and
@@ -65,11 +71,27 @@ func Parse(reader io.ReaderAt, size int64) (doc *Docx, err error) {
}
// Write allows to save a docx to a writer
func (f *Docx) Write(writer io.Writer) (err error) {
func (f *Docx) WriteTo(writer io.Writer) (_ int64, err error) {
zipWriter := zip.NewWriter(writer)
defer zipWriter.Close()
return f.pack(zipWriter)
return 0, f.pack(zipWriter)
}
// Read allows to save a docx to buf
func (f *Docx) Read(p []byte) (n int, err error) {
if !f.isbufempty {
n, err = f.buf.Read(p)
if err == io.EOF {
f.buf.Reset()
f.isbufempty = true
return
}
}
zipWriter := zip.NewWriter(f.buf)
defer zipWriter.Close()
f.isbufempty = false
return f.buf.Read(p)
}
// Refer gets the url for a reference