Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Compressing the file into GZIP format. 
 

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

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.