MCPcopy Create free account
hub / github.com/MertJSX/folderhost / Unzip

Function Unzip

utils/archive_functions.go:13–53  ·  view source on GitHub ↗
(src, dest string, cb func(int64, bool, string))

Source from the content-addressed store, hash-verified

11)
12
13func Unzip(src, dest string, cb func(int64, bool, string)) error {
14 r, err := zip.OpenReader(src)
15 if err != nil {
16 return fmt.Errorf("cannot open zip file: %v", err)
17 }
18 defer r.Close()
19
20 var (
21 totalSize int64 = 0
22 currentUID int = os.Getuid()
23 currentGID int = os.Getgid()
24 )
25
26 remainingFolderSpace, err := GetRemainingFolderSpace()
27
28 if err != nil {
29 return fmt.Errorf("cannot get remaining folder space: %v", err)
30 }
31
32 for _, file := range r.File {
33 cb(totalSize, false, "") // Parameters: totalSize, isCompleted, abortMsg
34 err := extractFile(file, dest, &totalSize, currentUID, currentGID)
35 if err != nil {
36 log.Printf("Unzip error: %v\n", err)
37 return fmt.Errorf("unable to extract file (%s): %v", file.Name, err)
38 }
39 if totalSize > remainingFolderSpace {
40 err := os.RemoveAll(dest)
41 if err != nil {
42 cb(totalSize, false, "Unzip process exceeds storage limit! Error while deleting the extracted folder.")
43 return fmt.Errorf("unzip process exceeds storage limit")
44 }
45 cb(totalSize, false, "Unzip process exceeds storage limit!")
46 return fmt.Errorf("unzip process exceeds storage limit")
47 }
48 }
49
50 cb(totalSize, true, "")
51
52 return nil
53}
54
55func extractFile(file *zip.File, dest string, totalSize *int64, uid int, gid int) error {
56 filePath := filepath.Join(dest, file.Name)

Callers 1

HandleUnzipFunction · 0.92

Calls 2

GetRemainingFolderSpaceFunction · 0.85
extractFileFunction · 0.85

Tested by

no test coverage detected