Master ZIP Creation and Extraction in Go Using archive/zip
This article explains how Go's archive/zip package enables developers to easily create and extract ZIP archives, covering core types, step‑by‑step code examples for writing and reading ZIP files, and a UML overview of the processing flow.
Introduction
Working with ZIP files is a common task in everyday development for data compression or packaging. Go's archive/zip package offers powerful functionality that lets developers create and extract ZIP archives with minimal effort. This article explores the main features of the package and provides example code to help readers understand its implementation.
Overview of Go's archive/zip Package
The archive/zip package allows reading and writing ZIP archive files. Its primary capabilities include creating new ZIP files and reading existing ones. Key types and methods are: Writer: used to create ZIP files. Reader: used to read ZIP files. File: represents a file inside a ZIP archive. OpenReader: opens a ZIP file for reading.
Creating a ZIP File
Use the zip.Writer type to generate a new ZIP archive. The basic steps are:
package main
import (
"archive/zip"
"bytes"
"os"
)
func main() {
// Create the output file
outFile, err := os.Create("example.zip")
if err != nil {
panic(err)
}
defer outFile.Close()
// Create a zip.Writer
zw := zip.NewWriter(outFile)
defer zw.Close()
// Files to add
var files = []struct {
Name, Body string
}{
{"readme.txt", "This is a readme file."},
{"hello.txt", "Hello, world!"},
}
for _, file := range files {
f, err := zw.Create(file.Name)
if err != nil {
panic(err)
}
_, err = f.Write([]byte(file.Body))
if err != nil {
panic(err)
}
}
}Extracting a ZIP File
Reading a ZIP archive involves the zip.Reader type. The following example demonstrates how to open a ZIP file and print the contents of each entry:
package main
import (
"archive/zip"
"fmt"
"io"
"os"
)
func main() {
// Open the ZIP file
r, err := zip.OpenReader("example.zip")
if err != nil {
panic(err)
}
defer r.Close()
// Iterate over each file in the archive
for _, f := range r.File {
fmt.Printf("Contents of %s:
", f.Name)
rc, err := f.Open()
if err != nil {
panic(err)
}
_, err = io.Copy(os.Stdout, rc)
rc.Close()
if err != nil {
panic(err)
}
fmt.Println()
}
}UML Diagram of ZIP Processing Flow
The following UML diagram visualizes the creation and extraction processes described above.
Conclusion
The Go archive/zip package provides a simple yet powerful interface for handling ZIP files. With the explanations and code samples in this article, readers should be able to incorporate ZIP creation and extraction into their Go applications, enabling efficient file compression and distribution.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
