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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Creating a ZIP file in Java 
 

This section teaches you about the ZIP file with java program.

 

Creating a ZIP file in Java

                         

Zip File: Zip file format is the popular method of data compression. Zip file contains many files in compressed format. You can say the files are stored in the zip format with compression. The file which is in the zip format holds the ".zip" extension.

You can make executable file in zip format. You can archive data from the zip file by extracting it. For create a zip file from other type of file, following program gives you the best way of making it possible through you code in java application.

Program Result:

This program makes a zip file from only one file which is given by you. It takes a file to make it into zip format and it takes the zip file name which has to be created from the given file. Finally, it makes the zip file. You can archive the file from the zip file by extracting, if you want.

Code Description:

Several methods and APIs are used in the following program for making a zip file from the given file. These are explained as follows:

ZipOutputStream:
ZipOutputStream is the class of java.util.zip.*; package. This class is used to compression the file whether the file is compressed on uncompressed.

ZipOutputStream.setLevel(Deflater.DEFAULT_COMPRESSION):
Above mentioned is the setLevel() method of ZipOutputStream class which sets the level of the compression process. This method takes a argument which is the built-in field of the Deflater class.

Deflater:
This is the class of java.util.zip.*; package. This class provides the compression using the ZLIB compression library.

Here is the code of the program:

import java.io.*;
import java.util.zip.*;

public class ZipCreateExample{
  public static void main(String[] argsthrows IOException{
    System.out.print("Please enter file name to zip : ");
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    String filesToZip = input.readLine();
    File f = new File(filesToZip);
    if(!f.exists()){
      System.out.println("File not found.");
      System.exit(0);
    }
    System.out.print("Please enter zip file name : ");
    String zipFileName = input.readLine();
    if (!zipFileName.endsWith(".zip"))
      zipFileName = zipFileName + ".zip";
    byte[] buffer = new byte[18024];
    try{
      ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
      out.setLevel(Deflater.DEFAULT_COMPRESSION);
      FileInputStream in = new FileInputStream(filesToZip);
      out.putNextEntry(new ZipEntry(filesToZip));
      int len;
      while ((len = in.read(buffer)) 0){
        out.write(buffer, 0, len);
      }
      out.closeEntry();
      in.close();
      out.close();
    }
    catch (IllegalArgumentException iae){
      iae.printStackTrace();
      System.exit(0);
    }
    catch (FileNotFoundException fnfe){
      fnfe.printStackTrace();
      System.exit(0);
    }
    catch (IOException ioe){
      ioe.printStackTrace();
      System.exit(0);
    }
  }
}

Download this example.

                         

» View all related tutorials
Related Tags: c com make this ie example to exam pic top e it complete in as sta m let pi xa

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.