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


 
  
 
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
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Zipping a File

                         

This example shows how to create a zip file in java. In other words, we will learn how to compress a file just to take less space. We can compress any file using various standalone applications. These standalone applications are nothing but the zip (compress or decompress )tools. Examples of zip tools are Winrar, WinZip etc. It is also possible to zip and unzip the files from your Java applications. This example shows how we zip a file through a java program.

The java.util.zip.DeflaterOutputStream class is immediate supper class of java.util.zip.ZipOutputStream class. java.util.zip. ZipOutputStream implements java.util.zip.ZipConstants interface. The class ZipOutStream implements an output stream filter to write files in the zip file format. This class can be used for both compressed and uncompressed files.

The java.io.FilterOutputStream class extends java.io.BufferedOutputStream class and implements a buffered output stream. We can write a byte steam into a file on the system. The java.io.FileInputStream class extends java.io.InputStream class. FileInputStream class is used to obtain input bytes from a file in a file system. The FileInputStream class is used to read stream of row bytes (such as image data), for reading streams of characters by considering FileReader class.

putNextEntry(new ZipEntry(filesToZip)):

This is the method of ZipOutputStream class which is used to enter files one by one to be zipped. This method is used to close previous zip entry if any active and create the next zip entry by passing the instance of the ZipEntry class which holds the file name to be zipped. In this example we are going to zip "profile.txt" into "outFile.zip" .To execute this example you first need to create profile.txt in same directory.

Here is the code of the program : 

import java.io.*;
import java.util.zip.*;
public class ZipFileExample
  {
public static void main(String a[])
  {
  try
    {
    ZipOutputStream out = new ZipOutputStream(new 
BufferedOutputStream
(new FileOutputStream("outFile.zip")));
    byte[] data = new byte[1000];               
    BufferedInputStream in = new BufferedInputStream
(
new FileInputStream("profile.txt"));
    int count;
   out.putNextEntry(new ZipEntry("outFile.zip"));
   while((count = in.read(data,0,1000)) != -1)
   {      
   out.write(data, 0, count);
   }
   in.close();
   out.flush();
   out.close();
  System.out.println("Your file is zipped");
 }
 catch(Exception e)
  {
 e.printStackTrace();
  }        
 }
}

Download this example.

                         

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 

Current Comments

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

This is brilliant site for java.
Please send me the code for making an application by which users can download videos etc.and code to build an application in which users can Upload some videos as well.

Thank you

Posted by Ashish on Tuesday, 01.15.08 @ 17:25pm | #45262

Latest Searches:
read and compare conte
Hash maps in struts2
objective type
JSTL : Submit Form Tex
create file menu using
new set
Arrays
code to make text box
java packages
Submit comments in dat
strut
TCP buffer
trim().length()
Java Count Vowels in t
validate radio button
StelsDBF - a JDBC driv
how to check the char
empty
insert data to text fi
clear grid dojo
DatabaseOracleOracleSQ
project for mobile pla
tree view
rich:pickList
How do you sort an arr
reading and writing ex
abstrct path name of f
<form: checkbox path=
spring:html tags examp
tokenizer
frame in html
jdbc with ms access
set colours Jtree
Insert path file into
hiding message box in
sum servlets program
ASP.NET .NET Security
user registration and
puzzle game in java
multiple ajax combo
input in java
java: read string arra
Text Field Midlet Exam
text editor wiyh menu
8 puzzle game in java
how to get applicate s
C and Cpp Programming
how to apend a file
Count Records using th
Hibernate Mapping
swap arrays in java

Java I/0 Examples
blob
File size in MB
how to pass same varia
param-name
eXist
login authentication
Photoshop Abstracts Ab
Photoshop Effects Abst
rest
websphere
Database MySQL Using S
invokeLater
pdf to xml using java
toString
Hibernate with DAO
pdf
array in javascript
jasper
code to convert pdf fi
jsp bean
login eample code usin
context attribute of a
populate dropdown in j
pl/sql
different uml technolo
call ?¢????¢??¬???Â
hiding frame in jsp
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 | 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.