skip to content
Alvin Lucillo

Decompress/unzip a gzip file in Go

/ 1 min read

💻 Tech

In Go, you can unzip a gzip file by using the gzip package, creating a reader with gzip.NewReader and passing the gzip file like so:

package main

import (
	"compress/gzip"
	"fmt"
	"io"
	"os"
)

func main() {
	// Open the gzip file
	gzipFile, err := os.Open("file.gz")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer gzipFile.Close()

	// Create a reader
	reader, err := gzip.NewReader(gzipFile)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer reader.Close()

    // Print the contents to the standard output
	_, err = io.Copy(os.Stdout, reader)
	if err != nil {
		fmt.Println(err)
		return
	}
}