aboutsummaryrefslogtreecommitdiff
path: root/internal/lume/zip.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/lume/zip.go')
-rw-r--r--internal/lume/zip.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/internal/lume/zip.go b/internal/lume/zip.go
new file mode 100644
index 0000000..b3b331f
--- /dev/null
+++ b/internal/lume/zip.go
@@ -0,0 +1,68 @@
+package lume
+
+import (
+ "archive/zip"
+ "io"
+ "os"
+ "path/filepath"
+)
+
+// ZipFolder takes a source folder and a target zip file name
+// and compresses the folder contents into the zip file
+func ZipFolder(source, target string) error {
+ // Create a zip file
+ fout, err := os.Create(target)
+ if err != nil {
+ return err
+ }
+ defer fout.Close()
+
+ // Create a zip writer
+ w := zip.NewWriter(fout)
+ defer w.Close()
+
+ // Walk through the source folder
+ return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
+ // Handle errors
+ if err != nil {
+ return err
+ }
+
+ // Skip directories
+ if info.IsDir() {
+ return nil
+ }
+
+ // Open the file
+ file, err := os.Open(path)
+ if err != nil {
+ return err
+ }
+ defer file.Close()
+
+ // Create a header from the file info
+ header, err := zip.FileInfoHeader(info)
+ if err != nil {
+ return err
+ }
+
+ // Set the compression method to deflate
+ header.Method = zip.Deflate
+
+ // Set the header name to the relative path of the file
+ header.Name, err = filepath.Rel(source, path)
+ if err != nil {
+ return err
+ }
+
+ // Create a fout for the file header
+ fout, err := w.CreateHeader(header)
+ if err != nil {
+ return err
+ }
+
+ // Copy the file contents to the writer
+ _, err = io.Copy(fout, file)
+ return err
+ })
+}