Data Compression with Zlib in Golang
Introduction
Zlib is a lossless data compression library used in many applications. It’s used in web servers, file compression utilities, and other applications where efficient data compression is needed. This library is fast, portable, and easy to use. This article will teach us how to compress and decompress data with Zlib in Go.
Golang has a built-in package called compress/zlib
that provides the functionality to compress and decompress data using the Zlib algorithm.
Zlib Compression
The compress/zlib
package provides a Writer
type used to compress data. The Writer
type implements the io.Writer
interface. Meaning we can use the Writer
type to compress data from any source that implements the io.Writer
interface, be it a file, a network connection, or a string.
In this example, we create a bytes.Buffer
to write our compressed data. We then make a new zlib.Writer
and pass in the buffer as the destination. We then write some uncompressed data to the Writer
and close it. This flushes the compressed data to the buffer. Finally, we write the compressed data to standard output.
If you check the output of the program, you will see that the compressed data is different from the uncompressed data.
Zlib Decompression
To decompress data, we use the Reader
type provided by the package. The Reader
type implements the io.Reader
interface. The NewReader
function implements the Reader
function and takes in an io.Reader
as the source of the compressed data.
In this example, we create a bytes.Buffer
and pass in the compressed data. We then create a new zlib.Reader
and pass it into the buffer as the compressed data source. We then write the decompressed data to standard out.
Data Compression over the Network
In the previous examples, we compressed and decompressed data from a buffer. In this example, we’ll compress and decompress data sent over the network.
This client-server example simulates that a user sends a message to a server. The client will first compress the message and send it to the server. The server will then decompress the message and print it to standard output.
In the server, we create a new zlib.Reader
and pass in the connection as the compressed data source. We then write the decompressed data to standard out. On the client, we create a new zlib.Writer
and pass in the connection as the destination. We then write the compressed data to the server.
By running the client and server, we can see that the data is sent over the network compressed, and decompressed on the server with no issues.
Conclusion
In this article, we explored how to compress and decompress data with Zlib in Go using the compress/zlib
package. Now armed with this knowledge, go forth and explore compressing other data formats like PNG that we never got to cover in this article.