The Gzip and Zlib algorithms are popularly used to compress and decompress files. Gzip is primarily used for compressing single files, while Zlib mainly compresses data streams. The algorithms use the Deflate Compression Algorithm for compression, although Gzip provides additional features including error checking and file metadata.

Gzip has provided better compression ratios than Zlib. However, Gzip requires more processing power to compress and decompress data. In most cases, you should use Gzip for compressing files and Zlib for compressing data streams.

Go provides the functionality for compressing data, including files through the compress package.

The Compress Package

contents of the compress package

The compress package supports compressing data through the compress/gzip and compress/zlib packages, amongst other functionalities.

The gzip package supports compressing and decompressing the gzip file format data, including read and write operations as specified in RFC 1952. While the zlib package helps with compressing and decompressing data in the zlib format.

You can import gzip and zlib from the compress package using the import keyword.

        import (
    "compress/gzip" // import gzip

    "compress/zlib" // import zlib
)

Compressing and Decompressing Files With Gzip

Gzip is a file format and a software application for file compression and decompression. Gzip uses the Lempel-Ziv-Markov chain algorithm (LZ77) to compress data, and the algorithm is often used for compressing text files, such as HTML, CSS, or JavaScript files.

The process of compressing files with the gzip package is simple and intuitive. You’ll need to open the file, create a gzipped file, create a gzip writer, and copy the original file's contents to the gzip writer before the flush operation that ensures the writing process is complete.

Run this bash command in the terminal of your working directory on Unix systems to create a sample text file and then insert text in the file.

        // creates a text file.
touch example.txt

// pipes the string to the file
echo 'Hello, world!' > example.txt}

After creating the file and inserting the text, you can import the gzip, io, and os packages for the compression operation.

Here is how you can use the compress/gzip package to compress a text file.

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

func main() {
    // Open the original file
    originalFile, err := os.Open("example.txt")
    if err != nil {
        panic(err)
    }
    defer originalFile.Close()

    // Create a new gzipped file
    gzippedFile, err := os.Create("example.txt.gz")
    if err != nil {
        panic(err)
    }
    defer gzippedFile.Close()

    // Create a new gzip writer
    gzipWriter := gzip.NewWriter(gzippedFile)
    defer gzipWriter.Close()

    // Copy the contents of the original file to the gzip writer
    _, err = io.Copy(gzipWriter, originalFile)
    if err != nil {
        panic(err)
    }

    // Flush the gzip writer to ensure all data is written
    gzipWriter.Flush()
}

The Open function of the os package opens the text file, and the Close function closes the file with a defer statement. The Create function creates a gzipped file, and the NewWriter function of the gzip package writes the content of the text file to the gzip file using the Copy function of the io package.

The Flush method of the gzipWriter instance flushes the gzip writer once all the data is available on the zipped file.

You can retrieve the original file from a gzipped file through a decompression process. The process of decompressing a file is similar; you’ll open the file and create a gzip file reader, then create a new file to hold the uncompressed data before copying the contents to the new file.

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

func main() {
    // Open the gzipped file
    gzippedFile, err := os.Open("example.txt.gz")
    if err != nil {
        panic(err)
    }
    defer gzippedFile.Close()

    // Create a new gzip reader
    gzipReader, err := gzip.NewReader(gzippedFile)
    defer gzipReader.Close()

    // Create a new file to hold the uncompressed data
    uncompressedFile, err := os.Create("example.txt")
    if err != nil {
        panic(err)
    }
    defer uncompressedFile.Close()

    // Copy the contents of the gzip reader to the new file
    _, err = io.Copy(uncompressedFile, gzipReader)
    if err != nil {
        panic(err)
    }
}

The Open function of the os package opens the gzipped file, and the NewReader function of the gzip package reads the zipped file. The Create function of the os package creates a new text file. The Copy function copies the contents of the gzipReader to the uncompressedFile.

Compressing and Decompressing Data With Zlib

Zlib is a library for data compression and decompression; the library also uses the LZ77 algorithm. Zlib is written in C and is widely used as the basis for other compression libraries and software. Unlike gzip, zlib is a library, and zlib does not include a file format. However, it is often used to compress data stored in container formats, such as PNG or HTTP.

The process of compressing with zlib is the same as for gzip. You’ll create a zlib file, configure a writer, open the original file, and copy the contents to the compressed file.

        import (
   "compress/zlib"
   "io"
   "os"
)

func main() {
   // Create a new file "example.zlib"
   file, err := os.Create("example.zlib")
   // If error occurs, panic and stop the program
   if err != nil {
       panic(err)
   }
   // Ensure that the file is closed after the function returns
   defer file.Close()

   // Create a new zlib writer with the best compression level
   writer, err := zlib.NewWriterLevel(file, zlib.BestCompression)
   // If error occurs, panic and stop the program
   if err != nil {
       panic(err)
   }
   // Ensure that the writer is closed after the function returns
   defer writer.Close()

   // Open the input file "example.txt"
   inputFile, err := os.Open("example.txt")
   // If error occurs, panic and stop the program
   if err != nil {
       panic(err)
   }
   // Ensure that the input file is closed after the function returns
   defer inputFile.Close()

   // Copy the contents of the input file to the writer
   io.Copy(writer, inputFile)
}

The Create method creates the zlib file, and the NewWriterLevel function creates a writer for the file with the specified option (in this case, the BestCompression option). The Open method of the os package opens the text file, and the Copy function of the io package copies the content of the text file to the zlib file in the compression process.

To decompress the zlib file, you’ll need to open the compressed file, create a new zlib reader, and then finally copy the content of the reader to standard output.

        import (
   "compress/zlib"
   "io"
   "os"
)

func main() {
   // Open the compressed file "compressed_file.zlib"
   file, err := os.Open("compressed_file.zlib")
   // If error occurs, panic and stop the program
   if err != nil {
       panic(err)
   }
   // Ensure that the file is closed after the function returns
   defer file.Close()

   // Create a new zlib reader for the compressed file
   reader, err := zlib.NewReader(file)
   // If error occurs, panic and stop the program
   if err != nil {
       panic(err)
   }
   // Ensure that the reader is closed after the function returns
   defer reader.Close()

   // Copy the contents of the reader to the standard output
   io.Copy(os.Stdout, reader)
}

The main function opens the zlib file with the os package’s Open function, and the NewReader function of the zlib package reads the zlib file into a reader instance. The Copy method of the io package copies the content from the reader to the standard output (in this case, the console).

output of text in the compressed file

Use These Tools for File Compression

Writing code for file compression is handy for automating tasks and compressing several files. If you only need to compress a few files, you can resort to using applications like WinRar, WinZip, Express Zip, and Bandizip.