Zip Folder Using Java

Here we will learn how to compress files and sub folders of a folder just to take less space.

Zipping a Folder


In this example we are going to create a zip folder and all files contained in this folder are zipped by using java. Here we will learn how to compress files and sub folders of a folder just to take less space.

To do so we create two objects of File class. In first object we are pass the name of  the folder which have to compress and in the second object we pass the name of zip folder.

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.

We are creating an array of bytes with 1000 size and an array of string which will hold the name of all files of folders. The list() method is used to find list of files in a folder. By using BufferOutputStream we write the files and data into a folder. Here getPath() method is used to get the path of the "inFolder".

putNextEntry(new ZipEntry(filesToZip))This is the method of ZipOutputStream class which is used to entry files one by one to be zipped. This method is used to close any previous active zip entry and create a new 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 "out" folder into "". To execute this you need first to create "out"  folder and put the files to be zipped into this folder.

Here is the code of the program : 

 public class ZipFolderExample
 public static void main(String a[])
 File inFolder=new File("out");
   File outFolder=new File("");
 ZipOutputStream out = new ZipOutputStream(new 
(new FileOutputStream(outFolder)));
 BufferedInputStream in = null;
 byte[] data  = new byte[1000];
 String files[] = inFolder.list();
 for (int i=0; i<files.length; i++)
  in = new BufferedInputStream(new FileInputStream
inFolder.getPath() "/" + files[i])1000);  
(new ZipEntry(files[i]))
  int count;
  while((count =,0,1000)) != -1)
 out.write(data, 0, count);
  catch(Exception e)

Download this example.

Share on Google+Share on Google+

Zip Folder 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 Folder Using Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
January 27, 2012
To zip a folder

To zip contents of a folder and its subfolders in Windows, replace, out.putNextEntry(new ZipEntry(files[i])); with out.putNextEntry(new ZipEntry(files[i]).replace(inFolder+"\\,""));
June 20, 2012
ZipFolder using Java

G8! Thanks it worked! Also plz post on how to use java.util.jar.Pack200 in order to compress a jar file.