Golang Code

Unzip Files in Go

· 255 words · 2 minutes read #archive #decompress #destination #file #filex #open #os #source #uncompress #unzip #zip

The code below is used to unzip, or decompress, a zip archive file into it’s constituent files. Because there are likely to be multiple files, it will create the files within a folder (specified with the 2nd parameter).

Also, try this code if you are wanting to create zip files.

This was based on code from here.

package main

import (

func main() {

    files, err := Unzip("test.zip", "output")
    if err != nil {

    fmt.Println("Unzipped:\n" + strings.Join(files, "\n"))

// Unzip will decompress a zip archive, moving all files and folders 
// within the zip file (parameter 1) to an output directory (parameter 2).
func Unzip(src string, dest string) ([]string, error) {

    var filenames []string

    r, err := zip.OpenReader(src)
    if err != nil {
        return filenames, err
    defer r.Close()

    for _, f := range r.File {

        rc, err := f.Open()
        if err != nil {
            return filenames, err
        defer rc.Close()

        // Store filename/path for returning and using later on
        fpath := filepath.Join(dest, f.Name)
        filenames = append(filenames, fpath)

        if f.FileInfo().IsDir() {

            // Make Folder
            os.MkdirAll(fpath, os.ModePerm)

        } else {

            // Make File
            if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
                return filenames, err

            outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
            if err != nil {
                return filenames, err

            _, err = io.Copy(outFile, rc)

            // Close the file without defer to close before next iteration of loop

            if err != nil {
                return filenames, err

    return filenames, nil
Image of Author Edd Turtle

Author: Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below. Thanks for reading!