diff options
author | Matthew Lemon <y@yulqen.org> | 2024-04-20 17:02:52 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-04-20 17:02:52 +0100 |
commit | 6925bc8676fb24fad7bebe7b7b921cc3a5bc5ab9 (patch) | |
tree | 0792731826e013f2cc7feeb9e806b8747053ff30 | |
parent | ed8a51dd320c5fb003f12003c7a66cfa353f6f81 (diff) |
wip: bad implementation of unzip
-rw-r--r-- | cmd/dbasik-api/return.go | 24 | ||||
-rw-r--r-- | cmd/dbasik-api/return_test.go | 12 | ||||
-rw-r--r-- | testdata/test.zip | bin | 0 -> 5274 bytes |
3 files changed, 36 insertions, 0 deletions
diff --git a/cmd/dbasik-api/return.go b/cmd/dbasik-api/return.go index 0973fc9..f172452 100644 --- a/cmd/dbasik-api/return.go +++ b/cmd/dbasik-api/return.go @@ -1,6 +1,7 @@ package main import ( + "archive/zip" "fmt" "github.com/tealeg/xlsx/v3" "path/filepath" @@ -133,6 +134,29 @@ func (fp *DirectoryFilePackage) Prepare() ([]string, error) { return files, nil } +type ZipFilePackage struct { + FilePath string +} + +func (fp *ZipFilePackage) Prepare() ([]string, error) { + // return a slice of the files from inside the zip file pointed to by fh.FilePath + // and an error if any + files, err := zip.OpenReader(fp.FilePath) + if err != nil { + return nil, err + } + defer files.Close() + out := []string{} + for _, file := range files.File { + out = append(out, file.Name) + } + return out, nil +} + func NewDirectoryFilePackage(filePath string) *DirectoryFilePackage { return &DirectoryFilePackage{FilePath: filePath} } + +func NewZipFilePackage(filePath string) *ZipFilePackage { + return &ZipFilePackage{FilePath: filePath} +} diff --git a/cmd/dbasik-api/return_test.go b/cmd/dbasik-api/return_test.go index bd2ffd9..d6c9daa 100644 --- a/cmd/dbasik-api/return_test.go +++ b/cmd/dbasik-api/return_test.go @@ -179,3 +179,15 @@ func TestPrepareFiles(t *testing.T) { t.Errorf("Prepare() did not return ../../testdata/valid_excel.xlsx") } } + +func TestUnzipFiles(t *testing.T) { + fp := NewZipFilePackage("../../testdata/test.zip") + files, err := fp.Prepare() + if err != nil { + t.Error(err) + } + t.Log(files) + if !slices.Contains(files, "valid_excel.xlsx") { + t.Errorf("Prepare() did not return test.xlsx") + } +} diff --git a/testdata/test.zip b/testdata/test.zip Binary files differnew file mode 100644 index 0000000..b712934 --- /dev/null +++ b/testdata/test.zip |