Home Java Beginners Compressing the file into GZIP format.
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Compressing the file into GZIP format.

Advertisement
This example will help to understand the concepts of GZIP.

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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 7, 2007

Related Tutorials

Ask Questions?    Discuss: Compressing the file into GZIP format.  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Jessie
September 24, 2011
kAjKNGvAkn

Stay with this guys, you're helping a lot of ppeloe.
Rajaganapathy
December 28, 2011
gzip algorithm

explain of gzip
jai
January 26, 2012
asking about removing errors

when i run that program igot many errors like that .......i have copied that -g Generate all debugging info -g:none Generate no debugging info -g:{lines,vars,source} Generate only some debugging info -nowarn Generate no warnings -verbose Output messages about what the compiler is doing -deprecation Output source locations where deprecated APIs are u sed -classpath <path> Specify where to find user class files -cp <path> Specify where to find user class files -sourcepath <path> Specify where to find input source files -bootclasspath <path> Override location of bootstrap class files -extdirs <dirs> Override location of installed extensions -endorseddirs <dirs> Override location of endorsed standards path -d <directory> Specify where to place generated class files -encoding <encoding> Specify character encoding used by source files -source <release> Provide source compatibility with specified release -target <release> Generate class files for specific VM version -version Version information -help Print a synopsis of standard options -X Print a synopsis of nonstandard options -J<flag> Pass <flag> directly to the runtime system
jai
January 26, 2012
about hw to remove error below given

opening the input stream Exception isjava.io.FileNotFoundException: hello (The system cannot find the fil e specified) comes after running that code
DMCA.com