Share on Google+Share on Google+

Create zip file in Java

In this section, you will learn about creating a zip file in java.

Create zip file in Java



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.


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 : 


public class ZipCreateExample{
public static void main(String[] args) throws IOException{
System.out.println("Example of ZIP file creation.");
System.out.println("Please enter file name to zip : ");
BufferedReader input = 
new BufferedReader(new InputStreamReader(;
String filesToZip = input.readLine();
File f = new File(filesToZip);
System.out.println("File not found.");
System.out.print("Please enter zip file namewith extension .zip : ");
String zipFileName = input.readLine();
byte[] buffer = new byte[18024];
ZipOutputStream out = 
new ZipOutputStream(new FileOutputStream(zipFileName));
FileInputStream in = new FileInputStream(filesToZip);
out.putNextEntry(new ZipEntry(filesToZip));
int len;
while ((len = > 0){
out.write(buffer, 0, len);
catch (IllegalArgumentException iae){
catch (FileNotFoundException fnfe){
catch (IOException ioe){

Download this example.



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

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Create zip file in Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Nishanth Thomas
December 1, 2011
Simplified code for zip file in java

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(); } }