golang

Gzip Compression in Go (Gzip)

File compression is an incredible feature that allows for a quick and very efficient storage and transfer of data across multiple mediums.

One of the most popular file and data stream compression technique is gzip. Gzip has been at the forefront of file compression since its introduction.

As developers, we are interested in the various ways of programmatically interacting with compression tools such as gzip.

In this tutorial, we will walk you through the basics of compression using gzip and the Go programming language.

What Is the Gzip Package?

In Go, the gzip package provides a convenient way to work with gzip-compressed data.

It allows us to compress and decompress the data using the gzip format without needing for external library since it is part of the compression/gzip standard library.

The package comes with various features such as:

  • Compression – We can use the “gzip.Writer” to compress the data and write it to an output stream.
  • Decompression – We can use the “gzip.Reader” to decompress the data that is read from an input stream.

Let us dive into using the gzip package in Go.

Compress the Data in Golang – Gzip

The first and most common use case of gzip is compressing the data. In Go, we can use the gzip package to compress the data.

The following example demonstrates how to compress a string and write it to a file:

package main
import (
    "compress/gzip"
    "fmt"
    "os"
)
func main() {
    text := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
    file, err := os.Create("compressed.txt.gz")
    if err != nil {
        panic(err)
    }
    defer file.Close()
    writer := gzip.NewWriter(file)
    defer writer.Close()
    _, err = writer.Write([]byte(text))
    if err != nil {
        panic(err)
    }

    fmt.Println("Data compressed and written to 'compressed.txt.gz'")
}

In the given example, we start by creating a string that we wish to compress.

We then create a new file which is “compressed.txt.gz” that holds the compressed data. Next, the “gzip.Writer” is used to create a writer that allows us to write to the file. We also use the “defer” keyword to ensure that we close the writer when done.

Finally, we write the text to the “gzip.Writer” and compress the data in the process.

Decompress the Data in Golang

The process of decompressing is pretty straightforward. We read the previously compressed data and decompress it as shown in the following example:

package main
import (
    "compress/gzip"
    "fmt"
    "io"
    "os"
)
func main() {
    file, err := os.Open("compressed.txt.gz")
    if err != nil {
        panic(err)
    }
    defer file.Close()
    reader, err := gzip.NewReader(file)
    if err != nil {
        panic(err)
    }
    defer reader.Close()
    decompressedData := make([]byte, 0)
    buffer := make([]byte, 1024) // Read buffer

    for {
        n, err := reader.Read(buffer)
        if err != nil && err != io.EOF {
            panic(err)
        }
        if n == 0 {
            break
        }
        decompressedData = append(decompressedData, buffer[:n]...)
    }
    fmt.Println("Decompressed Data:")
    fmt.Println(string(decompressedData))
}

In the given example code, we start by creating a “gzip.Reader” to read from the file. Next, we set up a loop to read the data from the “gzip.Reader” into a buffer.

The loop continues until we reach the end of the file (io.EOF) and we append the data that is read from the buffer to the “decompressedData” slice.

The resulting output is as follows:

Decompressed Data:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Conclusion

This tutorial covered the basics of working with the gzip package in Go to compress and decompress the data.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list