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)
In this example, we have a text file in /home/saket/Desktop/Lazyroar/compress.java drive under “Lazyroar” Folder and we are compressing and generating the GZip file in the same folder.
// Java program to compress a File // using GZIPOutputStream class import 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/Lazyroar/compress.gz" ; static final String INPUT_FILE = "/home/saket/Desktop//Lazyroar/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 GZIPOutputStream import 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/Lazyroar/compress.gz" ; static final String OUTPUT_FILE = "/home/saket/Desktop//Lazyroar/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 Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar 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.