Decompressing using the GZIP algorithm at runtime

Use the standard API class GZIPInputStream. Internally, it uses Inflater to decompress data. The following example asks the user a file to decompress and writes the decompressed stream of data to file.def.

Main.java:

import java.util.zip.*;
import java.io.*;
 
public class Main {
   public static void main(String args[]) {
      if (args.length != 1) {
         System.out.println("Usage: java Main <file.gzip>");
         System.out.println("<file.gzip>.def will be created");
         System.exit(1);
      }
 
      try {
         FileInputStream fis = new FileInputStream(args[0]);
         FileOutputStream fos = new FileOutputStream(args[0] + ".def");
 
         GZIPInputStream gis = new GZIPInputStream(fis);
         byte []buffer = new byte[1024];
         int l;
         while ((l = gis.read(buffer)) >= 0) {
            fos.write(buffer, 0, l);
         }
  
         gis.close();
         fos.close();
      }
      catch(IOException e) {
         System.out.println(e);
      }
   }
}