Extracting (unzip) archived files

The following example demonstrates how to unarchive a ZIP format file. It will replicate the directory structure it finds inside the archive by creating directories if necessary:

// This example uses zip but standard library // also supports tar archives package main import ( "archive/zip" "io" "log" "os" "path/filepath" ) func main() { // Create a reader out of the zip archive zipReader, err := zip.OpenReader("test.zip") if err != nil { log.Fatal(err) } defer zipReader.Close() // Iterate through each file/dir found in for _, file := range zipReader.Reader.File { // Open the file inside the zip archive // like a normal file zippedFile, err := file.Open() if err != nil { log.Fatal(err) } defer zippedFile.Close() ...

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.