From: Andrea Zagli Date: Wed, 3 Aug 2022 11:29:36 +0000 (+0200) Subject: Aggiunta la funzione CreaZip. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=d227b8e7412b079154768d30e4194ce1dfff31f7;p=zakegg_go Aggiunta la funzione CreaZip. --- diff --git a/zakegg.go b/zakegg.go index 5a6803e..b15c8c0 100644 --- a/zakegg.go +++ b/zakegg.go @@ -3,6 +3,10 @@ package zakegg import ( "fmt" "math" + "os" + "io" + "archive/zip" + "path/filepath" ) func Paginazione(start int, rows int, rows_x_page int, url string) string { @@ -111,3 +115,42 @@ func Paginazione(start int, rows int, rows_x_page int, url string) string { return ret } + +func CreaZip(filename_output string, filenames_input []string, only_basename bool) bool { + archive, err := os.Create(filename_output) + if err != nil { + fmt.Println(err) + return false + } + defer archive.Close() + + zipWriter := zip.NewWriter(archive) + + for _, fn := range filenames_input { + f1, err := os.Open(fn) + if err != nil { + fmt.Println(err) + continue + } + + var fname string + if only_basename { + fname = filepath.Base(fn) + } else { + fname = fn + } + w1, err := zipWriter.Create(fname) + if err != nil { + fmt.Println(err) + continue + } + if _, err := io.Copy(w1, f1); err != nil { + fmt.Println(err) + } + + f1.Close() + } + + zipWriter.Close() + return true +} diff --git a/zakegg_test.go b/zakegg_test.go new file mode 100644 index 0000000..1d4470b --- /dev/null +++ b/zakegg_test.go @@ -0,0 +1,16 @@ +package zakegg + +import ( + "testing" +) + +func TestCreaZip(t *testing.T) { + files := []string{"zakegg.go", "go.mod"} + + ret := CreaZip("test.zip", files) + + if !ret { + t.Log("File zip non creato correttamente") + t.Fail() + } +}