package main import ( "archive/zip" "os" ) func main() { zipFile, err := os.Create("myArchive.zip") if err != nil { panic(err) } defer zipFile.Close() archive := zip.NewWriter(zipFile) defer archive.Close() }
package main import ( "archive/zip" "os" ) func main() { zipFile, err := os.Create("myArchive.zip") if err != nil { panic(err) } defer zipFile.Close() archive := zip.NewWriter(zipFile) defer archive.Close() file1, err := os.Open("file1.txt") if err != nil { panic(err) } defer file1.Close() fileInfo1, err := file1.Stat() if err != nil { panic(err) } header1, err := zip.FileInfoHeader(fileInfo1) if err != nil { panic(err) } header1.Name = "file1.txt" header1.Method = zip.Deflate writer1, err := archive.CreateHeader(header1) if err != nil { panic(err) } _, err = io.Copy(writer1, file1) if err != nil { panic(err) } }
package main import ( "archive/zip" "fmt" "os" ) func main() { zipFile, err := zip.OpenReader("myArchive.zip") if err != nil { panic(err) } defer zipFile.Close() for _, file := range zipFile.File { fmt.Printf("Extracting %s\n", file.Name) destFile, err := os.Create(file.Name) if err != nil { panic(err) } defer destFile.Close() srcFile, err := file.Open() if err != nil { panic(err) } defer srcFile.Close() _, err = io.Copy(destFile, srcFile) if err != nil { panic(err) } } }The package library for `archive/zip` is part of the standard library in Go.