diff options
author | xuri <xuri.me@gmail.com> | 2021-12-27 23:34:14 +0800 |
---|---|---|
committer | xuri <xuri.me@gmail.com> | 2021-12-27 23:49:28 +0800 |
commit | 89b85934f60ba0012f3de6da03eb12959e4b4b72 (patch) | |
tree | 3d913c2bdabf06b79b7c2f223cfe26b64aacd8cc /lib.go | |
parent | 6b1e592cbc7b1412da5f6d0badeaf1083117c762 (diff) |
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
Diffstat (limited to 'lib.go')
-rw-r--r-- | lib.go | 14 |
1 files changed, 10 insertions, 4 deletions
@@ -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. |