Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/binary"
"encoding/xml"
"io"
"io/fs"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -136,8 +137,22 @@ func (f *File) WriteTo(w io.Writer, opts ...Options) (int64, error) {
// WriteToBuffer provides a function to get bytes.Buffer from the saved file,
// and it allocates space in memory. Be careful when the file size is large.
func (f *File) WriteToBuffer() (*bytes.Buffer, error) {
return f.WriteToBufferCustomZIP(func(w io.Writer) ZipWriter { return zip.NewWriter(w) })
}

type ZipWriter interface {
Create(name string) (io.Writer, error)
AddFS(fsys fs.FS) error
Close() error
}

// WriteToBufferCustomZIP provides a function to write to a buffer using a custom ZIP writer
// factory. This allows for custom ZIP compression implementations to be used when saving
// the spreadsheet to memory. The factory function should return a zipWriter interface
// that will be used to create the final ZIP archive.
func (f *File) WriteToBufferCustomZIP(factory func(io.Writer) ZipWriter) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
zw := zip.NewWriter(buf)
zw := factory(buf)

if err := f.writeToZip(zw); err != nil {
_ = zw.Close()
Expand All @@ -158,8 +173,8 @@ func (f *File) WriteToBuffer() (*bytes.Buffer, error) {
return buf, nil
}

// writeToZip provides a function to write to zip.Writer
func (f *File) writeToZip(zw *zip.Writer) error {
// writeToZip provides a function to write to zipWriter
func (f *File) writeToZip(zw ZipWriter) error {
f.calcChainWriter()
f.commentsWriter()
f.contentTypesWriter()
Expand Down