Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Create zip file in Java

                         

Introduction

In this section, you will know about a zip file. You will also learn how to create a zip file from any file through the java program.
 
A Zip file is a compressed format file and takes less space then the original file. Many files which are available for download on the internet are stored to a "ZIP" file. When the original files are needed, the user can "extract" the original files from the ZIP file using a ZIP file program.

It is possible to compress and decompress data using tools such as WinZip, gzip, and Java Archive (or jar), these tools are used as standalone applications. It is also possible to zip and unzip the files from your Java applications. This program shows you how to zip any file through your java program.

Our program accepts the file name and the output file name for creating the achieve. Program compresses the file using default compression level. The value of the compression level can be set using the setLevel(Deflater.DEFAULT_COMPRESSION) method of the ZipOutputStream class. 

Constructors and methods of ZipOutputStream:

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));

Above code creates the instance of the ZipOutputStream class by passing the FileOutpuStream instance. The zip file name (which has to be created) has also been mentioned in the FileOutputStream() constructor.

out.setLevel(Deflater.DEFAULT_COMPRESSION);

The setLevel() method of the ZipOutputStream class sets the default compression level. The method takes the argument Deflater.DEFAULT_COMPRESSION, which specific the  is compression level.

FileInputStream in = new FileInputStream(filesToZip);

Above code creates the instance of FileInputStream taking the input file name as parameter.

out.putNextEntry(new ZipEntry(filesToZip));

This is the putNextEntry() method of the ZipOutputStream class which is used to entry files one by one to be zipped. This method closes previous zip entry if any active and put the next zip entry by passing the instance of the ZipEntry class which holds the file name which has to be zipped.

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.println("Example of ZIP file creation.");
    System.out.println("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 namewith extension .zip : ");
    String zipFileName = input.readLine();
    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.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

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.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2008. All rights reserved.