Reading and writing ZIP files
ZIP compression is a widely used compression format. It is usual to use the ZIP format for an application to upload a file set or, on the other hand, export zipped files as output. This recipe will show you how to handle ZIP files programmatically with the use of the standard library.
How to do it...
- Open the console and create the folder
chapter05/recipe11
. - Navigate to the directory.
- Create the
zip.go
file with the following content:
package main import ( "archive/zip" "bytes" "fmt" "io" "io/ioutil" "log" "os" ) func main() { var buff bytes.Buffer // Compress content zipW := zip.NewWriter(&buff) f, err := zipW.Create("newfile.txt") if err != nil { panic(err) } _, err = f.Write([]byte("This is my file content")) if err != nil { panic(err) } ...