package main import ( "archive/zip" "fmt" "io" "os" ) func main() { // Open a zip archive for reading. r, err := zip.OpenReader("test.zip") if err != nil { fmt.Println(err) os.Exit(1) } defer r.Close() // Iterate through the files in the archive. for _, f := range r.File { fmt.Printf("File: %s\n", f.Name) // Open the file for reading. rc, err := f.Open() if err != nil { fmt.Println(err) os.Exit(1) } defer rc.Close() // Display the file contents. _, err = io.Copy(os.Stdout, rc) if err != nil { fmt.Println(err) os.Exit(1) } fmt.Println() } }
package main import ( "archive/zip" "fmt" "io" "os" ) func main() { // Create a new zip archive. f, err := os.Create("test.zip") if err != nil { fmt.Println(err) os.Exit(1) } defer f.Close() // Create a new zip writer. zw := zip.NewWriter(f) // Add files to the zip archive. for _, file := range []string{"file1.txt", "file2.txt"} { w, err := zw.Create(file) if err != nil { fmt.Println(err) os.Exit(1) } _, err = io.WriteString(w, "Hello, world!") if err != nil { fmt.Println(err) os.Exit(1) } } // Close the zip writer. err = zw.Close() if err != nil { fmt.Println(err) os.Exit(1) } }Package Library: The package library used for these examples is "archive/zip".