Skip to content

Commit

Permalink
Construct XlsxFile from zip.Reader (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
xakep666 authored Apr 6, 2021
1 parent 87507f9 commit 87e8730
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
41 changes: 36 additions & 5 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type XlsxFile struct {

// XlsxFileCloser wraps XlsxFile to be able to close an open file
type XlsxFileCloser struct {
zipReadCloser zip.ReadCloser
zipReadCloser *zip.ReadCloser
XlsxFile
}

Expand Down Expand Up @@ -72,14 +72,32 @@ func OpenFile(filename string) (*XlsxFileCloser, error) {

x := new(XlsxFile)

if err := x.init(zipFile.Reader); err != nil {
if err := x.init(&zipFile.Reader); err != nil {
zipFile.Close()
return nil, err
}

return &XlsxFileCloser{
XlsxFile: *x,
zipReadCloser: *zipFile,
zipReadCloser: zipFile,
}, nil
}

// OpenReaderZip takes the zip ReadCloser of an XLSX file and returns a populated XlsxFileCloser struct for it.
// If the file cannot be found, or key parts of the files contents are missing, an error
// is returned.
// Note that the file must be Close()-d when you are finished with it.
func OpenReaderZip(rc *zip.ReadCloser) (*XlsxFileCloser, error) {
x := new(XlsxFile)

if err := x.init(&rc.Reader); err != nil {
rc.Close()
return nil, err
}

return &XlsxFileCloser{
XlsxFile: *x,
zipReadCloser: rc,
}, nil
}

Expand All @@ -93,15 +111,28 @@ func NewReader(xlsxBytes []byte) (*XlsxFile, error) {
}

x := new(XlsxFile)
err = x.init(*r)
err = x.init(r)
if err != nil {
return nil, err
}

return x, nil
}

func (x *XlsxFile) init(zipReader zip.Reader) error {
// NewReaderZip takes zip reader of Xlsx file and returns a populated XlsxFile struct for it.
// If the file cannot be found, or key parts of the files contents are missing, an error
// is returned.
func NewReaderZip(r *zip.Reader) (*XlsxFile, error) {
x := new(XlsxFile)

if err := x.init(r); err != nil {
return nil, err
}

return x, nil
}

func (x *XlsxFile) init(zipReader *zip.Reader) error {
sharedStrings, err := getSharedStrings(zipReader.File)
if err != nil {
return err
Expand Down
29 changes: 29 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ func TestOpeningXlsxFile(t *testing.T) {
require.Equal(t, []string{"datarefinery_groundtruth_400000"}, f.Sheets)
}

func TestOpeningZipReadCloser(t *testing.T) {
zrc, err := zip.OpenReader("./test/test-small.xlsx")
require.NoError(t, err)
defer zrc.Close()

f, err := OpenReaderZip(zrc)
defer f.Close()

require.NoError(t, err)
require.Equal(t, []string{"datarefinery_groundtruth_400000"}, f.Sheets)
}

func TestClosingFile(t *testing.T) {
actual, err := OpenFile("./test/test-small.xlsx")
require.NoError(t, err)
Expand All @@ -72,6 +84,23 @@ func TestNewReaderFromXlsxBytes(t *testing.T) {
require.Equal(t, []string{"datarefinery_groundtruth_400000"}, actual.Sheets)
}

func TestNewZipReader(t *testing.T) {
f, err := os.Open("./test/test-small.xlsx")
require.NoError(t, err)

defer f.Close()

fstat, err := f.Stat()
require.NoError(t, err)

zr, err := zip.NewReader(f, fstat.Size())
require.NoError(t, err)

xlsx, err := NewReaderZip(zr)
require.NoError(t, err)
require.Equal(t, []string{"datarefinery_groundtruth_400000"}, xlsx.Sheets)
}

func TestDeletedSheet(t *testing.T) {
actual, err := OpenFile("./test/test-deleted-sheet.xlsx")

Expand Down

0 comments on commit 87e8730

Please sign in to comment.