Spaces:
Paused
Paused
| package utils | |
| import ( | |
| "archive/zip" | |
| "io" | |
| "os" | |
| "path/filepath" | |
| ) | |
| // ZipDirectory zips the contents of source directory into target zip file | |
| func ZipDirectory(source, target string) error { | |
| zipFile, err := os.Create(target) | |
| if err != nil { | |
| return err | |
| } | |
| defer zipFile.Close() | |
| archive := zip.NewWriter(zipFile) | |
| defer archive.Close() | |
| info, err := os.Stat(source) | |
| if err != nil { | |
| return err | |
| } | |
| var baseDir string | |
| if info.IsDir() { | |
| baseDir = source | |
| } else { | |
| baseDir = filepath.Dir(source) | |
| } | |
| return filepath.Walk(source, func(path string, info os.FileInfo, err error) error { | |
| if err != nil { | |
| return err | |
| } | |
| // Create a header based on the file info | |
| header, err := zip.FileInfoHeader(info) | |
| if err != nil { | |
| return err | |
| } | |
| // Relativize the path | |
| relPath, err := filepath.Rel(baseDir, path) | |
| if err != nil { | |
| return err | |
| } | |
| // Use forward slashes for cross-platform compatibility | |
| header.Name = filepath.ToSlash(relPath) | |
| if info.IsDir() { | |
| header.Name += "/" | |
| } else { | |
| header.Method = zip.Deflate | |
| } | |
| writer, err := archive.CreateHeader(header) | |
| if err != nil { | |
| return err | |
| } | |
| if info.IsDir() { | |
| return nil | |
| } | |
| file, err := os.Open(path) | |
| if err != nil { | |
| return err | |
| } | |
| defer file.Close() | |
| _, err = io.Copy(writer, file) | |
| return err | |
| }) | |
| } | |