Working with ZIP Archives in Go (Golang) — Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with ZIP Archives in Go (Golang)



In Go (Golang), working with ZIP archives is implemented through the standard archive/zip package. This package provides all the necessary tools for creating, reading, writing, and extracting files from ZIP archives. Unlike many other languages, Go does not require installing third-party libraries for basic operations — everything is already in the standard library.



The archive/zip package supports:

  • Creating new ZIP archives
  • Reading existing archives
  • Adding files and directories
  • Extracting individual files or the entire archive
  • Working with file metadata (date, permissions)


Creating a ZIP Archive in Go



To create an archive, you need to open a file for writing and create a zip.Writer. Each file is added via Create or CreateHeader for fine-tuning.



package main

import ( "archive/zip" "fmt" "io" "os")

func main() { // Create a new ZIP file zipFile, err := os.Create("example.zip") if err != nil { panic(err) } defer zipFile.Close()

// Create a Writer for the archive zipWriter := zip.NewWriter(zipFile) defer zipWriter.Close()

// Add a text file file1, err := zipWriter.Create("hello.txt") if err != nil { panic(err) } _, err = file1.Write([]byte("Hello, Go ZIP!")) if err != nil { panic(err) }

// Add a second file with content from an existing file sourceFile, err := os.Open("source.txt") if err != nil { panic(err) } defer sourceFile.Close()

targetFile, err := zipWriter.Create("copy_of_source.txt") if err != nil { panic(err) } _, err = io.Copy(targetFile, sourceFile) if err != nil { panic(err) }

fmt.Println("Archive created: example.zip")}


Reading a ZIP Archive



To read an archive, use zip.OpenReader, which returns a ReadCloser. Each file is represented by the zip.File structure.



package main

import ( "archive/zip" "fmt" "io" "os")

func main() { // Open the archive for reading reader, err := zip.OpenReader("example.zip") if err != nil { panic(err) } defer reader.Close()

// Iterate over all files in the archive for _, file := range reader.File { fmt.Printf("File: %s", file.Name) fmt.Printf("Size: %d bytes", file.UncompressedSize64) fmt.Printf("Compressed size: %d bytes", file.CompressedSize64) fmt.Printf("Modification date: %s", file.Modified.Format("2006-01-02 15:04:05")) fmt.Println("---")

// Open the file from the archive rc, err := file.Open() if err != nil { panic(err) } defer rc.Close()

// Read the content (if it's a small file) content, err := io.ReadAll(rc) if err != nil { panic(err) } fmt.Printf("Content: %s

", string(content)) }}


Extracting Files from an Archive



To fully extract an archive into a directory, you need to create target folders and write the contents of each file.



package main

import ( "archive/zip" "fmt" "io" "os" "path/filepath")

func extractZip(src, dest string) error { reader, err := zip.OpenReader(src) if err != nil { return err } defer reader.Close()

for _, file := range reader.File { // Safe file name (preventing path traversal) filePath := filepath.Join(dest, file.Name) // Check for escaping the target directory if !strings.HasPrefix(filepath.Clean(filePath),

Recommendations