This article shows how to use Java to compress a file in Gzip format.

1. What is Gzip?
GZip is a standard file compression tool on Unix or Linux system, and generally has the suffix .gz.

2. Why we need to compress a file in Gzip?
Answer: Smaller file size, save disk space.

3. How about tar.gz?
Gzip compress a single file, and the Tar is collecting files into one archive file, visit this article to Create tar.gz in Java

1. Compress a file in Gzip – GZIPOutputStream

We copy the FileInputStream into a GZIPOutputStream to compress a file in Gzip format.

1.1 The below example compresses a file sitemap.xml into a Gzip file sitemap.xml.gz.

GZipExample.java

package com.favtuts.io.howto.compress;

import java.io.*;
import java.nio.file.*;
import java.util.zip.*;

public class GZipExample {
    public static void main(String[] args) {

        // compress a file
        Path source = Paths.get("/home/tvt/workspace/favtuts/sitemap.xml");
        Path target = Paths.get("/home/tvt/workspace/favtuts/sitemap.xml.gz");

        if (Files.notExists(source)) {
            System.err.printf("The path %s doesn't exist!", source);
            return;
        }

        try {

            GZipExample.compressGzip(source, target);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // copy file (FileInputStream) to GZIPOutputStream
    public static void compressGzip(Path source, Path target) throws IOException {

        try (GZIPOutputStream gos = new GZIPOutputStream(
                                      new FileOutputStream(target.toFile()));
             FileInputStream fis = new FileInputStream(source.toFile())) {

            // copy file
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) > 0) {
                gos.write(buffer, 0, len);
            }

        }

    }
}

Output, the XML file is compressed in Gzip format, and the file size drops from 256K to 1.3K.

$ pwd
/home/tvt/workspace/favtuts
$ ls -lsah sitemap.*
256K -rw-rw-r-- 1 tvt tvt 255K Thg 5  20 16:15 sitemap.xml
4,0K -rw-rw-r-- 1 tvt tvt 1,4K Thg 5  20 16:16 sitemap.xml.gz

$ gzip -l sitemap.xml.gz
         compressed        uncompressed  ratio uncompressed_name
               1394              260295  99.5% sitemap.xml

1.2 This example is similar to example 1.1. Instead, we use NIO Files.copy to copy a Path to GZIPOutputStream directly.

  public static void compressGzipNio(Path source, Path target) throws IOException {

      try (GZIPOutputStream gos = new GZIPOutputStream(
                                    new FileOutputStream(target.toFile()))) {

          Files.copy(source, gos);

      }

  }

2. Compress a String to Gzip

2.1 This example compresses a String into a Gzip file.

  public static void compressStringToGzip(String data, Path target) throws IOException {

      try (GZIPOutputStream gos = new GZIPOutputStream(
                                    new FileOutputStream(target.toFile()))) {

          gos.write(data.getBytes(StandardCharsets.UTF_8));

      }

  }

Further Reading

Java – How to decompress a Gzip file

Download Source Code

$ git clone https://github.com/favtuts/java-core-tutorials-examples

$ cd java-io/howto/compress

References

Leave a Reply

Your email address will not be published. Required fields are marked *