Compressing the file into GZIP format.

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()
method.
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:
import java.io.*;
import java.util.zip.*;
public class CompressingFile {
public static void doCompressFile(String inFileName){
try{
File file = new File(inFileName);
System.out.println(" you are going to gzip the : " + file + "file");
FileOutputStream fos = new FileOutputStream(file + ".gz");
System.out.println(" Now the name of this gzip file is : " + file + ".gz" );
GZIPOutputStream gzos = new GZIPOutputStream(fos);
System.out.println(" opening the input stream");
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
System.out.println("Transferring file from" + inFileName + " to " + file + ".gz");
byte[] buffer = new byte[1024];
int i;
while ((i = in.read(buffer)) >= 0){
gzos.write(buffer,0,i);
}
System.out.println(" file is in now gzip format");
in.close();
gzos.close();
}
catch(IOException e){
System.out.println("Exception is" + e);
}
}
public static void main(String args[]){
if(args.length!=1){
System.err.println("Please enter the file name which needs to be compressed ");
}
else{
doCompressFile(args[0]);
}
}
}
|
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.
C:\java>java CompressingFile
hello.txt
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
C:\java> |
Download this program

|