Skip to content

Commit

Permalink
added UnzipToMemFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Jan 17, 2024
1 parent cc6661c commit 5f79fe2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func copyRecursive(ctx context.Context, src, dest File, patterns []string, buf *
}

if dest.Exists() && !dest.IsDir() {
return fmt.Errorf("Can't copy a directory (%s) over a file (%s)", src.URL(), dest.URL())
return fmt.Errorf("can not copy a directory (%s) over a file (%s)", src.URL(), dest.URL())
}

// TODO better check
Expand Down
35 changes: 35 additions & 0 deletions memfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fs

import (
"archive/zip"
"bytes"
"context"
"encoding/gob"
Expand Down Expand Up @@ -341,3 +342,37 @@ func (i memFileInfo) Mode() iofs.FileMode { return 0666 }
func (i memFileInfo) ModTime() time.Time { return time.Now() }
func (i memFileInfo) IsDir() bool { return false }
func (i memFileInfo) Sys() any { return nil }

// UnzipToMemFiles unzips the passed zipFile as MemFiles.
func UnzipToMemFiles(ctx context.Context, zipFile FileReader) ([]MemFile, error) {
fileReader, err := zipFile.OpenReadSeeker()
if err != nil {
return nil, err
}
defer fileReader.Close()

zipReader, err := zip.NewReader(fileReader, zipFile.Size())
if err != nil {
return nil, err
}

memFiles := make([]MemFile, len(zipReader.File))
for i, zipFile := range zipReader.File {
if strings.HasSuffix(zipFile.Name, "/") {
continue
}
r, err := zipFile.Open()
if err != nil {
return nil, err
}
memFiles[i], err = ReadAllMemFile(ctx, r, zipFile.Name)
if err != nil {
return nil, err
}
err = r.Close()
if err != nil {
return nil, err
}
}
return memFiles, nil
}

0 comments on commit 5f79fe2

Please sign in to comment.