This example will help to understand the concepts of GZIP. GZIP compresses the actual size of data files. It is a file compression utility program which can be found very easily found on internet. Its resultant file will have a .gz extension. Its main advantage is that it provides excellent levels of compression. It can be used where you want to save the file in less available area. It will compress the file to the great extent. It is a part of java.util.zip package.
To make a program on compressing a file, first make a class named Compressing
file. Declare a method doCompressFile(), which takes one String argument.
To compress a file we need a file, so make a object of File class, inside
the method. Use FileOutputStream class for writing a data to a file. Make
a object of FileOutputStream class and add the instance of File class to
the constructor of FileOutputStream class with extension .gz so, that the
resultant file will have the extension .gz. Now create the object of GZIPOutputStream
class . This class is responsible for writing data in the GZIP file format and
pass a reference of FileOutputStream class. Now create a buffered input
stream out of the file, which we are trying to add into the gzip archive.
After that we have to make a buffer so that the bytes can be read from the file.
After creating a buffer create a variable i of type int which will be
responsible for keeping track of how much we are reading each time. Use the
while loop to read from the file and write to the gzip archive file. At last use
the main method in which we will call the doCompressFile()
In this class we have used various classes and methods which we are describing below:
File: This class implements Serializable and Comparable interfaces.
CompressingFile: This class extends OutputStream. It is used for writing data to a File.
GZIPOutputStream: This class is a part of java.util.zip package. It extends DeflaterOutputStream class. This class write compressed data in the GZIP format.
FileInputStream: This class extends InputStream. It takes bytes from a file .
BufferedInputStream: As soon as we will create a object of BufferedInputStream an buffer is created. It causes all the bytes be read.
read(): It returns int. It reads the buffer.
write(byte buf, int off, int len i): It is a method of GZIPOutputStream, which takes three arguments. It is used for writing a array of bytes to the compressed file.
close(): It closes all the resources occupied by the InputStream
close(): It closes all the resources occupied by the GZIPOutputStream.
The code of the program is given below:
The output of the program is given below.
Create a file hello.txt which you want to
compress. After compressing the file we can see that the extension becomes .gz.
This is the compressed file.
you are going to gzip the : hello.txt file
Now the name of this gzip file is : hello.txt.gz
opening the input stream
Transferring file from hello.txt to hello.txt.gz
file is in now gzip format
Recommend the tutorial