This example will help you to understand the concepts of GZIP. Sometimes it happens that when you download a file or any PDF file you get it in compressed mode. This means that the file is compressed, but can't read it in compressed form so, it needs to be uncompressed. There are various uncompress ion utility program which can be found very easily found on internet. If you are uncompressing a PDF with the extension .gz, then its resultant file will have a .PDF extension. Its main advantage is that it will uncompress the PDF file so that you can be able to read it. This utility is a part of java.util.zip package which provides classes and methods to compress and decompress the file.
To make a program on uncompressing a file, firstly you need to import java.util.zip package so, that all classes and method of this package can be used, also import java.io package for the input/output operation. Then create a class named JavaUncompress. Declare a main method inside the class which will take one String argument i.e. file name. If the user forgets to type the name of the input file which has to be uncompressed, then ask the user to enter the file name. After getting the file name, we need to uncompress that particular file. For that we need to open the gzip file. Print a message so that you can understand where gzip file is opening. To open the input file firstly create a object of FileInputStream class which and pass the instance of FileInputStream class into the constructor of GZIPInputStream class, which will be created by using new operator. After opening the input file now we need to open the output file so that the data can be transferred from the input file to the output file. Print the message so that you can understand where the output is being written. For that use make a object of FileOutputStream class and pass the name of the resultant file in its constructor. For transferring the bytes from the compressed file to the output file declare a array of type byte with the size of 1024 bytes. It can be changed according to the needs of a programmer, now declare one variable of type int which will read the byte from the object of GZIPInputStream class. At last close the file and stream.
GZIPInputStream: This class is a part of java.util.zip package. It extends DeflaterInputStream class. This class reads compressed data in the GZIP format.
FileInputStream: This class extends InputStream. It reads bytes from a file .
FileOutputStream: This class extends OutputStream class. It is a output stream for writing data to a file.
read(): It returns int. It reads the buffer.
write(byte buf, int off, int len i): It is a method of OutputStream, which takes three arguments. It is used for writing a array of bytes to the uncompressing file.
close(): It closes all the resources occupied by the GZIPInputStream
close(): It closes all the resources occupied by the OutputStream.
The code of the program is given below:
The output of the program is given below.
In the output we can see that firstly we have
given the name of the compressed file. The file name should ends with .gz
extension, while opening the compressed gzip file we are printing that the file
is opening. After opening the file we are opening a file to which we have to
write the data from the compressed file and that also is printed in the output.
For transferring the bytes from the compressed file to the output file we are
printing the message. Before closing the file and stream we are again printing
|C:\java>java JavaUncompress C:\javaExample\unCompress\jsp.pdf.gz
Opening the gzip file.......................... : opened
Opening the output file............. : opened
Transferring bytes from the compressed file to the output file........ : Transfer successful
The file and stream is ......closing.......... : closed
Recommend the tutorial