Tutorial

Java GZIP Example - Compress and Decompress File

Published on August 4, 2022
author

Pankaj

Java GZIP Example - Compress and Decompress File

Welcome to Java GZIP example. GZIP is one of the favorite tool to compress file in Unix systems. We can compress a single file in GZIP format but we can’t compress and archive a directory using GZIP like ZIP files.

Java GZIP

GZIP, Java GZIP Example Here is a simple java GZIP example program showing how can we compress a file to GZIP format and then decompress the GZIP file to create a new file. GZIPExample.java

package com.journaldev.files;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPExample {

    public static void main(String[] args) {
        String file = "/Users/pankaj/sitemap.xml";
        String gzipFile = "/Users/pankaj/sitemap.xml.gz";
        String newFile = "/Users/pankaj/new_sitemap.xml";
        
        compressGzipFile(file, gzipFile);
        
        decompressGzipFile(gzipFile, newFile);
               
    }

    private static void decompressGzipFile(String gzipFile, String newFile) {
        try {
            FileInputStream fis = new FileInputStream(gzipFile);
            GZIPInputStream gis = new GZIPInputStream(fis);
            FileOutputStream fos = new FileOutputStream(newFile);
            byte[] buffer = new byte[1024];
            int len;
            while((len = gis.read(buffer)) != -1){
                fos.write(buffer, 0, len);
            }
            //close resources
            fos.close();
            gis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

    private static void compressGzipFile(String file, String gzipFile) {
        try {
            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(gzipFile);
            GZIPOutputStream gzipOS = new GZIPOutputStream(fos);
            byte[] buffer = new byte[1024];
            int len;
            while((len=fis.read(buffer)) != -1){
                gzipOS.write(buffer, 0, len);
            }
            //close resources
            gzipOS.close();
            fos.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

}

While decompressing a GZIP file, if it’s not in GZIP format, following exception will be thrown.

java.util.zip.ZipException: Not in GZIP format
	at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:164)
	at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:78)
	at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:90)
	at com.journaldev.files.GZIPExample.decompressGzipFile(GZIPExample.java:25)
	at com.journaldev.files.GZIPExample.main(GZIPExample.java:18)

That’s all for Java GZIP Example.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
February 7, 2018

Excellent example. Thanks for sharing.

- Mayur Musrif

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 14, 2016

    Hi When compressing the file, before you gzipOS.close() run gzipOS.finish(). It should help.

    - Tomek

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      October 31, 2014

      Hey can u please mail me the explanation of this program… step wise…

      - Rituraj Dharwadkar

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        June 17, 2014

        This post is very helpful. Could you please make a sample gzip xml file available for download. Plus, is there an easy way to generate a gzip json file? Thanks,

        - longlife

          Try DigitalOcean for free

          Click below to sign up and get $200 of credit to try our products over 60 days!

          Sign up

          Join the Tech Talk
          Success! Thank you! Please check your email for further details.

          Please complete your information!

          Become a contributor for community

          Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

          DigitalOcean Documentation

          Full documentation for every DigitalOcean product.

          Resources for startups and SMBs

          The Wave has everything you need to know about building a business, from raising funding to marketing your product.

          Get our newsletter

          Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

          New accounts only. By submitting your email you agree to our Privacy Policy

          The developer cloud

          Scale up as you grow — whether you're running one virtual machine or ten thousand.

          Get started for free

          Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

          *This promotional offer applies to new accounts only.