From 89b85934f60ba0012f3de6da03eb12959e4b4b72 Mon Sep 17 00:00:00 2001 From: xuri Date: Mon, 27 Dec 2021 23:34:14 +0800 Subject: This closes #1096, memory usage optimization and another 4 changes - Unzip shared string table to system temporary file when large inner XML, reduce memory usage about 70% - Remove unnecessary exported variable `XMLHeader`, we can using `encoding/xml` package's `xml.Header` instead of it - Using constant instead of inline text for default XML path - Rename exported option field `WorksheetUnzipMemLimit` to `UnzipXMLSizeLimit` - Unit test and documentation updated --- lib.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'lib.go') diff --git a/lib.go b/lib.go index 0efc180..8ec121b 100644 --- a/lib.go +++ b/lib.go @@ -30,8 +30,8 @@ func (f *File) ReadZipReader(r *zip.Reader) (map[string][]byte, int, error) { var ( err error docPart = map[string]string{ - "[content_types].xml": "[Content_Types].xml", - "xl/sharedstrings.xml": "xl/sharedStrings.xml", + "[content_types].xml": defaultXMLPathContentTypes, + "xl/sharedstrings.xml": dafaultXMLPathSharedStrings, } fileList = make(map[string][]byte, len(r.File)) worksheets int @@ -47,9 +47,15 @@ func (f *File) ReadZipReader(r *zip.Reader) (map[string][]byte, int, error) { if partName, ok := docPart[strings.ToLower(fileName)]; ok { fileName = partName } + if strings.EqualFold(fileName, dafaultXMLPathSharedStrings) && fileSize > f.options.UnzipXMLSizeLimit { + if tempFile, err := f.unzipToTemp(v); err == nil { + f.tempFiles.Store(fileName, tempFile) + continue + } + } if strings.HasPrefix(fileName, "xl/worksheets/sheet") { worksheets++ - if fileSize > f.options.WorksheetUnzipMemLimit && !v.FileInfo().IsDir() { + if fileSize > f.options.UnzipXMLSizeLimit && !v.FileInfo().IsDir() { if tempFile, err := f.unzipToTemp(v); err == nil { f.tempFiles.Store(fileName, tempFile) continue @@ -120,7 +126,7 @@ func (f *File) readTemp(name string) (file *os.File, err error) { // saveFileList provides a function to update given file content in file list // of spreadsheet. func (f *File) saveFileList(name string, content []byte) { - f.Pkg.Store(name, append([]byte(XMLHeader), content...)) + f.Pkg.Store(name, append([]byte(xml.Header), content...)) } // Read file content as string in a archive file. -- cgit v1.2.1