Zip File Using Java

Here, you will learn to compress a file. You can compress any file using various standalone applications. These standalone applications are nothing but the zip tools.

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 class is immediate supper class of class. ZipOutputStream implements 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 class extends class and implements a buffered output stream. We can write a byte steam into a file on the system. The class extends 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 "" .To execute this example you first need to create profile.txt in same directory.

Here is the code of the program : 

public class ZipFileExample
public static void main(String a[])
  ZipOutputStream out = new ZipOutputStream(new 
(new FileOutputStream("")));
  byte[] data = new byte[1000]
  BufferedInputStream in = new BufferedInputStream
new FileInputStream("profile.txt"));
  int count;
 out.putNextEntry(new ZipEntry(""));
 while((count =,0,1000)) != -1)
 out.write(data, 0, count);
  System.out.println("Your file is zipped");
 catch(Exception e)

Download this example.

Share on Google+Share on Google+

Zip File Using Java

Posted on: June 19, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Zip File Using Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 12, 2011
java io

please explain java io
June 27, 2011

hi hope it works
Nishanth Thomas
December 1, 2011
Simple code

import*; import*; public class ZipCreateExample{ /** * @param args the command line arguments * @throws FileNotFoundException */ public static void main(String[] args) throws Exception { // input file FileInputStream in = new FileInputStream("F:/Documents and Settings/nishanth1125/Desktop/");; // out put file ZipOutputStream out = new ZipOutputStream(new FileOutputStream("F:/Documents and Settings/nishanth1125/Desktop/")); // name of file out.putNextEntry(new ZipEntry("")); byte[] b = new byte[1024]; int count; while ((count = > 0) { System.out.println(); // write file in zip out.write(b, 0, count); } out.close(); in.close(); } }
July 17, 2012
Zipping a File

Instead of zipping a file, i want to zip a folder present on some location on my p.c (i.e D:\\java\pics) Is it possible? Any code-snippet will highly be appreciated.