Skip to content

Commit 2ed5b28

Browse files
committed
Use AddFS instead of manual ZIP file creation
1 parent d024424 commit 2ed5b28

File tree

2 files changed

+8
-49
lines changed

2 files changed

+8
-49
lines changed

acquisition/secure.go

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,55 +28,14 @@ func createZipFile(sourceDir, zipPath string) error {
2828
zipWriter := zip.NewWriter(zipFile)
2929
defer zipWriter.Close()
3030

31-
return filepath.Walk(sourceDir, func(filePath string, info os.FileInfo, err error) error {
32-
if err != nil {
33-
return fmt.Errorf("error walking path %s: %v", filePath, err)
34-
}
35-
36-
// Skip the root directory itself
37-
if filePath == sourceDir {
38-
return nil
39-
}
40-
41-
// Get the relative path from the source directory
42-
relPath, err := filepath.Rel(sourceDir, filePath)
43-
if err != nil {
44-
return fmt.Errorf("failed to get relative path for %s: %v", filePath, err)
45-
}
46-
47-
// Convert Windows backslashes to forward slashes
48-
zipEntryPath := strings.ReplaceAll(relPath, "\\", "/")
49-
50-
// Create directory entries for directories
51-
if info.IsDir() {
52-
zipEntryPath += "/"
53-
_, err := zipWriter.Create(zipEntryPath)
54-
if err != nil {
55-
return fmt.Errorf("failed to create directory entry %s: %v", zipEntryPath, err)
56-
}
57-
return nil
58-
}
59-
60-
// Create file entry
61-
fileWriter, err := zipWriter.Create(zipEntryPath)
62-
if err != nil {
63-
return fmt.Errorf("failed to create file entry %s: %v", zipEntryPath, err)
64-
}
65-
66-
// Copy file content
67-
file, err := os.Open(filePath)
68-
if err != nil {
69-
return fmt.Errorf("failed to open file %s: %v", filePath, err)
70-
}
71-
defer file.Close()
72-
73-
_, err = io.Copy(fileWriter, file)
74-
if err != nil {
75-
return fmt.Errorf("failed to copy file content for %s: %v", filePath, err)
76-
}
31+
// Use AddFS to add the entire directory
32+
fsys := os.DirFS(sourceDir)
33+
err = zipWriter.AddFS(fsys)
34+
if err != nil {
35+
return fmt.Errorf("failed to add directory to ZIP: %v", err)
36+
}
7737

78-
return nil
79-
})
38+
return nil
8039
}
8140

8241
func (a *Acquisition) StoreSecurely() error {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/mvt-project/androidqf
22

3-
go 1.20
3+
go 1.22
44

55
require (
66
filippo.io/age v1.1.1

0 commit comments

Comments
 (0)