MCPcopy
hub / github.com/moonD4rk/HackBrowserData / CompressDir

Function CompressDir

utils/fileutil/fileutil.go:27–54  ·  view source on GitHub ↗

CompressDir compresses the directory into a zip file

(dir string)

Source from the content-addressed store, hash-verified

25
26// CompressDir compresses the directory into a zip file
27func CompressDir(dir string) error {
28 files, err := os.ReadDir(dir)
29 if err != nil {
30 return fmt.Errorf("read dir error: %w", err)
31 }
32 if len(files) == 0 {
33 return fmt.Errorf("no files to compress in: %s", dir)
34 }
35
36 buffer := new(bytes.Buffer)
37 zipWriter := zip.NewWriter(buffer)
38 defer func() {
39 _ = zipWriter.Close()
40 }()
41
42 for _, file := range files {
43 if err := addFileToZip(zipWriter, filepath.Join(dir, file.Name())); err != nil {
44 return fmt.Errorf("failed to add file to zip: %w", err)
45 }
46 }
47
48 if err := zipWriter.Close(); err != nil {
49 return fmt.Errorf("error closing zip writer: %w", err)
50 }
51
52 zipFilename := filepath.Join(dir, filepath.Base(dir)+".zip")
53 return writeFile(buffer, zipFilename)
54}
55
56func addFileToZip(zw *zip.Writer, filename string) error {
57 content, err := os.ReadFile(filename)

Callers 2

extractAndWriteFunction · 0.92
TestCompressDirFunction · 0.85

Calls 3

addFileToZipFunction · 0.85
writeFileFunction · 0.85
ErrorfMethod · 0.80

Tested by 1

TestCompressDirFunction · 0.68