@@ -21,6 +21,7 @@ import (
2121 "io/ioutil"
2222 "os"
2323 "path"
24+ "path/filepath"
2425 "strconv"
2526 "strings"
2627 "sync"
@@ -59,21 +60,27 @@ type charsetTranscoderFn func(charset string, input io.Reader) (rdr io.Reader, e
5960
6061// Options define the options for open spreadsheet.
6162type Options struct {
62- Password string
63+ Password string
64+ UnzipSizeLimit int64
6365}
6466
65- // OpenFile take the name of an spreadsheet file and returns a populated spreadsheet file struct
66- // for it. For example, open spreadsheet with password protection:
67+ // OpenFile take the name of an spreadsheet file and returns a populated
68+ // spreadsheet file struct for it. For example, open spreadsheet with
69+ // password protection:
6770//
6871// f, err := excelize.OpenFile("Book1.xlsx", excelize.Options{Password: "password"})
6972// if err != nil {
7073// return
7174// }
7275//
73- // Note that the excelize just support decrypt and not support encrypt currently, the spreadsheet
74- // saved by Save and SaveAs will be without password unprotected.
76+ // Note that the excelize just support decrypt and not support encrypt
77+ // currently, the spreadsheet saved by Save and SaveAs will be without
78+ // password unprotected.
79+ //
80+ // UnzipSizeLimit specified the unzip size limit in bytes on open the
81+ // spreadsheet, the default size limit is 16GB.
7582func OpenFile (filename string , opt ... Options ) (* File , error ) {
76- file , err := os .Open (filename )
83+ file , err := os .Open (filepath . Clean ( filename ) )
7784 if err != nil {
7885 return nil , err
7986 }
@@ -89,6 +96,7 @@ func OpenFile(filename string, opt ...Options) (*File, error) {
8996// newFile is object builder
9097func newFile () * File {
9198 return & File {
99+ options : & Options {UnzipSizeLimit : UnzipSizeLimit },
92100 xmlAttr : make (map [string ][]xml.Attr ),
93101 checked : make (map [string ]bool ),
94102 sheetMap : make (map [string ]string ),
@@ -111,10 +119,13 @@ func OpenReader(r io.Reader, opt ...Options) (*File, error) {
111119 return nil , err
112120 }
113121 f := newFile ()
114- if bytes .Contains (b , oleIdentifier ) && len (opt ) > 0 {
115- for _ , o := range opt {
116- f .options = & o
122+ for i := range opt {
123+ f .options = & opt [i ]
124+ if f .options .UnzipSizeLimit == 0 {
125+ f .options .UnzipSizeLimit = UnzipSizeLimit
117126 }
127+ }
128+ if bytes .Contains (b , oleIdentifier ) {
118129 b , err = Decrypt (b , f .options )
119130 if err != nil {
120131 return nil , fmt .Errorf ("decrypted file failed" )
@@ -124,8 +135,7 @@ func OpenReader(r io.Reader, opt ...Options) (*File, error) {
124135 if err != nil {
125136 return nil , err
126137 }
127-
128- file , sheetCount , err := ReadZipReader (zr )
138+ file , sheetCount , err := ReadZipReader (zr , f .options )
129139 if err != nil {
130140 return nil , err
131141 }
@@ -316,18 +326,18 @@ func (f *File) UpdateLinkedValue() error {
316326 // recalculate formulas
317327 wb .CalcPr = nil
318328 for _ , name := range f .GetSheetList () {
319- xlsx , err := f .workSheetReader (name )
329+ ws , err := f .workSheetReader (name )
320330 if err != nil {
321331 if err .Error () == fmt .Sprintf ("sheet %s is chart sheet" , trimSheetName (name )) {
322332 continue
323333 }
324334 return err
325335 }
326- for indexR := range xlsx .SheetData .Row {
327- for indexC , col := range xlsx .SheetData .Row [indexR ].C {
336+ for indexR := range ws .SheetData .Row {
337+ for indexC , col := range ws .SheetData .Row [indexR ].C {
328338 if col .F != nil && col .V != "" {
329- xlsx .SheetData .Row [indexR ].C [indexC ].V = ""
330- xlsx .SheetData .Row [indexR ].C [indexC ].T = ""
339+ ws .SheetData .Row [indexR ].C [indexC ].V = ""
340+ ws .SheetData .Row [indexR ].C [indexC ].T = ""
331341 }
332342 }
333343 }
@@ -381,7 +391,7 @@ func (f *File) AddVBAProject(bin string) error {
381391 Type : SourceRelationshipVBAProject ,
382392 })
383393 }
384- file , _ := ioutil .ReadFile (bin )
394+ file , _ := ioutil .ReadFile (filepath . Clean ( bin ) )
385395 f .Pkg .Store ("xl/vbaProject.bin" , file )
386396 return err
387397}
0 commit comments