Compressing and Decompressing files using GZIP Format in java

02 Jun, 2017
The java.util.zip package provides classes compress and decompress the file contents. FileInputStream, FileOutputStream and GZIPOutputStream classes are provided in Java to compress and decompress the files.
Compressing a File using GZIPOutputStream
Methods used in the program
read(): Reads a byte of data. Present in FileInputStream.int read()write(): Writes a byte of data, Present in FileOutputStream.void write(int b) 
read(): Reads a byte of data. Present in FileInputStream.int read()
int read()
write(): Writes a byte of data, Present in FileOutputStream.void write(int b) 
void write(int b) 
In this example, we have a text file in /home/saket/Desktop/GeeksforGeeks/compress.java drive under “GeeksforGeeks” Folder and we are compressing and generating the GZip file in the same folder.
// Java program to compress a File// using GZIPOutputStream classimport java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.zip.GZIPOutputStream;  public class GeeksForGeeks{    static final String OUTPUT_FILE            = "/home/saket/Desktop/GeeksforGeeks/compress.gz";    static final String INPUT_FILE            = "/home/saket/Desktop//GeeksforGeeks/compress.java";          static void compress()    {        byte[] buffer = new byte[1024];        try        {            GZIPOutputStream os =                     new GZIPOutputStream(new FileOutputStream(OUTPUT_FILE));                                  FileInputStream in =                    new FileInputStream(INPUT_FILE);                          int totalSize;            while((totalSize = in.read(buffer)) > 0 )            {                os.write(buffer, 0, totalSize);            }                          in.close();            os.finish();            os.close();                          System.out.println("File Successfully compressed");        }        catch (IOException e)        {            e.printStackTrace();        }              }          public static void main (String[] args)    {        compress();              }}
Output:
File Successfully compressed

After Running the above program, It will compress the compress.java file:
Decompressing a File using GZIPOutputStream
// Java program to illustrate // Decompressing a File using GZIPOutputStreamimport java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.zip.GZIPInputStream;  class GeeksForGeeks{    static final String INPUT_FILE             = "/home/saket/Desktop/GeeksforGeeks/compress.gz";    static final String OUTPUT_FILE             = "/home/saket/Desktop//GeeksforGeeks/decompress.java";          static void decompress()    {        byte[] buffer = new byte[1024];        try        {            GZIPInputStream is =                     new GZIPInputStream(new FileInputStream(INPUT_FILE));                                  FileOutputStream out =                    new FileOutputStream(OUTPUT_FILE);                          int totalSize;            while((totalSize = is.read(buffer)) > 0 )            {                out.write(buffer, 0, totalSize);            }                          out.close();            is.close();                          System.out.println("File Successfully decompressed");        }        catch (IOException e)        {            e.printStackTrace();        }              }          public static void main (String[] args)    {        decompress();              }}
Output :
File Successfully decompressed

Current State after compiling:
Note : Here decom.java contains the above code.After Running the above program, It will decompress the compress.gz and a file named decompress.java will be created.
This article is contributed by Saket Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java-I/O
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.