From 38ad20efc11c1872c4e62a12617f0300c138b867 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 7 May 2018 16:12:51 +0800 Subject: save bytes on memory instead of string --- lib.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'lib.go') diff --git a/lib.go b/lib.go index c0426d6..a168781 100644 --- a/lib.go +++ b/lib.go @@ -12,8 +12,8 @@ import ( // ReadZipReader can be used to read an XLSX in memory without touching the // filesystem. -func ReadZipReader(r *zip.Reader) (map[string]string, int, error) { - fileList := make(map[string]string) +func ReadZipReader(r *zip.Reader) (map[string][]byte, int, error) { + fileList := make(map[string][]byte) worksheets := 0 for _, v := range r.File { fileList[v.Name] = readFile(v) @@ -27,21 +27,24 @@ func ReadZipReader(r *zip.Reader) (map[string]string, int, error) { } // readXML provides function to read XML content as string. -func (f *File) readXML(name string) string { +func (f *File) readXML(name string) []byte { if content, ok := f.XLSX[name]; ok { return content } - return "" + return []byte{} } // saveFileList provides function to update given file content in file list of // XLSX. -func (f *File) saveFileList(name, content string) { - f.XLSX[name] = XMLHeader + content +func (f *File) saveFileList(name string, content []byte) { + newContent := make([]byte, 0, len(XMLHeader)+len(content)) + newContent = append(newContent, []byte(XMLHeader)...) + newContent = append(newContent, content...) + f.XLSX[name] = newContent } // Read file content as string in a archive file. -func readFile(file *zip.File) string { +func readFile(file *zip.File) []byte { rc, err := file.Open() if err != nil { log.Fatal(err) @@ -49,7 +52,7 @@ func readFile(file *zip.File) string { buff := bytes.NewBuffer(nil) io.Copy(buff, rc) rc.Close() - return string(buff.Bytes()) + return buff.Bytes() } // ToAlphaString provides function to convert integer to Excel sheet column -- cgit v1.2.1