create zip archive

This commit is contained in:
rmanach 2022-04-12 14:09:20 +02:00
parent 304f720c79
commit b9885bed81
2 changed files with 74 additions and 3 deletions

4
.gitignore vendored
View File

@ -1,2 +1,4 @@
# binary file
packer
packer
# zip file
*.zip

73
main.go
View File

@ -3,8 +3,10 @@
package main
import (
"archive/zip"
"fmt"
"github.com/akamensky/argparse"
"io"
"log"
"os"
fp "path/filepath"
@ -48,7 +50,7 @@ func walkDirFiltered(directory string, regex re.Regexp) ([]string, error) {
return err
}
if !info.IsDir() && regex.MatchString(path) {
fmt.Println(path)
log.Println("file found :", path)
files = append(files, path)
}
return err
@ -62,6 +64,70 @@ func getFilesFromDirectoryPath(directory string, regex re.Regexp) ([]string, err
return files, err
}
// writeFileContent - write file content into zipped file
// chunking is used to avoid memory errors with large files
func writeFileContent(f *os.File, zf io.Writer) (err error) {
const chunkSize = 4
b := make([]byte, chunkSize)
for {
_, err := f.Read(b)
if err != nil {
if err != io.EOF {
continue
}
break
}
zf.Write(b)
}
return err
}
// generateZip - create a zip archive
// * outputPath: zip archive full path
// * filePaths: files path
func generateZip(outputPath string, filepaths []string) (err error) {
// create a zipfile from `outputPath`
zipPath, err := os.Create(outputPath)
if err != nil {
return err
}
defer zipPath.Close()
// create a new zip archive from `zipPath`
w := zip.NewWriter(zipPath)
for _, filepath := range filepaths {
// open matching regex file
f, err := os.Open(filepath)
if err != nil {
log.Println("[ERROR] :", err)
continue
}
// create a zipped file
zf, err := w.Create(filepath)
if err != nil {
log.Println("[ERROR] :", err)
f.Close()
continue
}
// write file content into zipped file
err = writeFileContent(f, zf)
// clean up opened file
f.Close()
}
// make sure to check the error on Close.
err = w.Close()
return err
}
func main() {
// parse arguments
@ -83,6 +149,9 @@ func main() {
log.Printf("looking for file '%s' in '%s' ...", rd.String(), directory)
files, err := getFilesFromDirectoryPath(directory, rd)
// generating zip archive
log.Printf("files found : %d", len(files))
log.Printf("files zipped in %s", outputPath)
generateZip(outputPath, files)
log.Printf("zip archive generated in %s", outputPath)
}