Uncompressing the file in the GZIP format.

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:
|
import java.util.zip.GZIPInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class JavaUncompress{
public static void main(String args[]){
try{
//To Uncompress GZip File Contents we need to open the gzip file.....
if(args.length<=0){
System.out.println("Please enter the valid file name");
}
else{
String inFilename = args[0];
System.out.println("Opening the gzip file........
.................. : opened");
GZIPInputStream gzipInputStream = null;
FileInputStream fileInputStream = null;
gzipInputStream = new GZIPInputStream(new
FileInputStream(inFilename));
System.out.println("Opening the output file..
........... : opened");
String outFilename = inFilename +".pdf";
OutputStream out = new FileOutputStream(outFilename);
System.out.println("Transferring bytes from the
compressed file to the output file........:
Transfer successful");
byte[] buf = new byte[1024]; //size can be
changed according to programmer's need.
int len;
while ((len = gzipInputStream.read(buf)) > 0) {
out.write(buf, 0, len);
}
System.out.println("The file and stream is ..
....closing.......... : closed");
gzipInputStream.close();
out.close();
}
}
catch(IOException e){
System.out.println("Exception has been thrown" + e);
}
}
}
|
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
the message.
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
C:\java> |
Download this example.

|
Current Comments
0 comments so far (post your own) View All Comments Latest 10 Comments: