Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Compressing and Decompressing Data with Apache Commons Codec

Header Image

Ahoy there, mateys! Welcome aboard our journey to explore the exciting world of Apache Commons Codec. Today, we’ll be discussing the art of compressing and decompressing data. This can come in handy when we need to send data over a network or store it on disk efficiently. Our focus will be on the Zlib compression and decompression feature of Apache Commons Codec.

But before we dive into the details, let’s have a quick review of what Apache Commons Codec is all about.

Overview of Apache Commons Codec

Apache Commons Codec is a library of encoding and decoding utilities for use in Java projects. It provides a set of easy-to-use methods for handling different encoding and decoding techniques such as Base64, hexadecimal, URL, and MIME. It also offers features for data compression and decompression, encryption and decryption, generating random numbers, and more.

The Apache Commons Codec library is an open-source project that has been around since 2002. It has evolved over the years to become a reliable and widely used library in the Java community. Its development is driven by a community of contributors who constantly update and maintain the library to keep it relevant.

Why Use Apache Commons Codec?

Now you may be wondering, “Why use Apache Commons Codec when there are other libraries available for data compression and decompression?” Well, for starters, Apache Commons Codec is easy to use and requires minimal code to achieve the desired result. Its features are well-documented, which makes it easy to understand and integrate into your project.

Another advantage of using Apache Commons Codec is its versatility. It provides a wide range of encoding and decoding techniques, which can come in handy when working with different types of data. Its data compression and decompression features are particularly useful when working with large amounts of data, such as multimedia files or network traffic.

Zlib Compression and Decompression

Now, let’s talk about Zlib compression and decompression. Zlib is a widely used data compression library that provides fast compression and decompression algorithms. It is commonly used in the gzip and PNG image formats, among others.

To use Zlib compression and decompression with Apache Commons Codec, we need to create an instance of the ZlibCodec class. We can then use this instance to compress or decompress data using the compress or decompress methods, respectively.

Here’s an example of compressing and decompressing a string using Zlib compression:

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.MessageDigestAlgorithms;
import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.codec.digest.Sha2Crypt;
import org.apache.commons.codec.digest.Sha3Crypt;
import org.apache.commons.codec.digest.BCrypt;
import org.apache.commons.codec.digest.Crypt;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.Pbkdf2PasswordEncoder;
import org.apache.commons.codec.binary.Hex;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class ZlibCompressionExample {

    public static void main(String[] args) throws UnsupportedEncodingException {

        String input = "Ahoy, mateys! We be compressin' and decompressin' some data today!";
        byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);

        // Create a ZlibCodec instance for compression
        ZlibCodec zlibCodec = new ZlibCodec(ZlibCodec.MODE_DEFLATE);

        // Compress the input bytes
        byte[] compressedBytes = zlibCodec.compress(inputBytes);

        // Print// Print the compressed bytes as a hexadecimal string
        System.out.println("Compressed string: " + Hex.encodeHexString(compressedBytes));

        // Create a ZlibCodec instance for decompression
        ZlibCodec zlibCodec2 = new ZlibCodec(ZlibCodec.MODE_INFLATE);

        // Decompress the compressed bytes
        byte[] decompressedBytes = zlibCodec2.decompress(compressedBytes);

        // Convert the decompressed bytes to a string
        String output = new String(decompressedBytes, StandardCharsets.UTF_8);

        // Print the original string and the decompressed string
        System.out.println("Original string: " + input);
        System.out.println("Decompressed string: " + output);
    }
}

As you can see, the ZlibCodec class makes it easy to compress and decompress data using the Zlib algorithm. In our example, we compressed a string and then decompressed the resulting bytes to get back the original string.

Gzip Compression and Decompression

While Zlib compression and decompression are great, sometimes we need a more efficient method for compressing data. That’s where gzip compression and decompression come in. Gzip is a file format and software application used for file compression and decompression.

To use gzip compression and decompression with Apache Commons Codec, we need to create an instance of the GzipCodec class. We can then use this instance to compress or decompress data using the compress or decompress methods, respectively.

Here’s an example of compressing and decompressing a string using gzip compression:

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.MessageDigestAlgorithms;
import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.codec.digest.Sha2Crypt;
import org.apache.commons.codec.digest.Sha3Crypt;
import org.apache.commons.codec.digest.BCrypt;
import org.apache.commons.codec.digest.Crypt;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.Pbkdf2PasswordEncoder;
import org.apache.commons.codec.binary.Hex;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GzipCompressionExample {

    public static void main(String[] args) throws IOException {

        String input = "Shiver me timbers! We be usin' some gzip compression and decompression now!";
        byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);

        // Create a GzipCodec instance for compression
        GzipCodec gzipCodec = new GzipCodec();

        // Compress the input bytes
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
        gzipOutputStream.write(inputBytes);
        gzipOutputStream.close();

        byte[] compressedBytes = outputStream.toByteArray();

        // Print the compressed bytes
        System.out.println("Compressed bytes: " + Hex.encodeHexString(compressedBytes));

        // Create a GzipCodec instance for decompression
        GzipCodec gzipCodec2 = new GzipCodec();

        // Decompress the compressed bytes
        ByteArrayInputStream inputStream = new ByteArrayInputStream(compressedBytes);
        GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        int length;
        byte[] buffer = new byte[1024];
        while ((length = gzipInputStream.read(buffer)) > 0) {
            byteArrayOutputStream.write(buffer, 0, length);
        }

        byte[] decompressedBytes = byteArrayOutputStream.toByteArray();

        // Print the decompressed string
        System.out.println("Decompressed string: " + new String(decompressedBytes, StandardCharsets.UTF_8));

    }

}

As you can see, using gzip compression and decompression is fairly simple with Apache Commons Codec. We just need to create an instance of the GzipCodec class and use the compress and decompress methods to achieve our desired result.

Conclusion

And there you have it, me hearties! We’ve explored the world of data compression and decompression with Apache Commons Codec. We learned about the Zlib and gzip compression algorithms and how to use them in our Java projects. With the help of Apache Commons Codec, we can now send and store data more efficiently, like true swashbucklers! So hoist the Jolly Roger and set sail for new coding adventures!