aboutsummaryrefslogtreecommitdiff
path: root/internal/lume/zip.go
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2023-10-22 12:01:20 -0400
committerXe Iaso <me@xeiaso.net>2023-10-22 12:01:20 -0400
commitc1534ebde902e2daf665caa95427043f0d63ef9d (patch)
tree60124b9cb3b4c63009a96e27e4f2bd49b184e707 /internal/lume/zip.go
parent2648995f5bf9c2d70f563288da9a36cb8f7cb5cc (diff)
downloadxesite-a/b.tar.xz
xesite-a/b.zip
internal/lume: store and serve the website from a .zip filea/b
This will let the website continue to serve data while a rebuild is happening. This also lets me download the website corpus for local debugging should the worst happen. Signed-off-by: Xe Iaso <me@xeiaso.net>
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
+ })
+}