MCPcopy Create free account
hub / github.com/auth0/auth0-cli / Unzip

Function Unzip

internal/utils/unzip.go:14–64  ·  view source on GitHub ↗

Unzip extracts files from a zip archive to a specified destination directory.

(src, dest string)

Source from the content-addressed store, hash-verified

12
13// Unzip extracts files from a zip archive to a specified destination directory.
14func Unzip(src, dest string) error {
15 r, err := zip.OpenReader(src)
16 if err != nil {
17 return err
18 }
19
20 defer func(r *zip.ReadCloser) {
21 _ = r.Close()
22 }(r)
23
24 for _, f := range r.File {
25 filPath := filepath.Join(dest, f.Name)
26
27 relPath, err := filepath.Rel(dest, filPath)
28 if err != nil || strings.Contains(relPath, ".."+string(os.PathSeparator)) {
29 return fmt.Errorf("illegal file path: %s", filPath)
30 }
31
32 if f.FileInfo().IsDir() {
33 if err := os.MkdirAll(filPath, os.ModePerm); err != nil {
34 return err
35 }
36 continue
37 }
38
39 if err := os.MkdirAll(filepath.Dir(filPath), os.ModePerm); err != nil {
40 return err
41 }
42
43 inFile, err := f.Open()
44 if err != nil {
45 return err
46 }
47
48 outFile, err := os.OpenFile(filPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
49 if err != nil {
50 _ = inFile.Close()
51 return err
52 }
53
54 _, err = io.Copy(outFile, inFile)
55 _ = inFile.Close()
56 _ = outFile.Close()
57
58 if err != nil {
59 return err
60 }
61 }
62
63 return nil
64}

Callers 5

DownloadMethod · 0.92
TestUnzip_SuccessFunction · 0.85
TestUnzip_IllegalPathFunction · 0.85
TestUnzip_InvalidFileFunction · 0.85

Calls 2

CloseMethod · 0.80
ErrorfMethod · 0.80

Tested by 3

TestUnzip_SuccessFunction · 0.68
TestUnzip_IllegalPathFunction · 0.68
TestUnzip_InvalidFileFunction · 0.68