package main import ( "archive/zip" "os" ) func main() { file, err := os.Create("new.zip") if err != nil { panic(err) } defer file.Close() archive := zip.NewWriter(file) defer archive.Close() }
package main import ( "archive/zip" "os" ) func main() { zipFile, err := os.Open("existing.zip") if err != nil { panic(err) } defer zipFile.Close() filesToAdd := []string{"file1.txt", "file2.txt", "file3.txt"} archive, err := zip.OpenWriter(zipFile) if err != nil { panic(err) } defer archive.Close() for _, file := range filesToAdd { f, err := os.Open(file) if err != nil { panic(err) } defer f.Close() info, err := os.Stat(file) if err != nil { panic(err) } header, err := zip.FileInfoHeader(info) if err != nil { panic(err) } w, err := archive.CreateHeader(header) if err != nil { panic(err) } if _, err := io.Copy(w, f); err != nil { panic(err) } } }Package Library: archive/zip.