Create ZIP (Compressed) file in Java with example code

If you don’t have winzip, don’t worry. You can develop your own winzip. This tutorial gives you the idea that how to create a zip file in java with multiple entries. Using the code, you can reduce the file size by compressing the file before copy or upload and uncompress the file later when you need. Now let us see how to write a compressed (ZIP) file in java. Zip file contains one or more Compressed file. For writing ZIP file, we have to use ZipOutputStream class from package.
                  ZipOutputStream zipFile=new ZipOutputStream(OutputStream out);
This class implements an output stream filter for writing files in the ZIP file format. The constructor accepts one argument of type OutputStream. The following lines creates a file.

File testZip=new File("c:/temp/zip","")
FileOutputStream fileObject=new FileOutputStram(testZip) 

which creates file in the folder c:/temp/zip

ZipOutputStream zipFile=new ZipOutputStream (fileObject);
which creates a zip file object which corresponds to file in the folder c:/temp/zip

Now Let us see how to add entry of file names to be compressed in the Zip File. That is done by another class ZipEntry . Each file in the Zip is identified by a Zip Entry. ZipEntry class accepts a String object which is individual file name to be compressed. We can add these files entry to the ZipFile by using the following statement.

ZipEntry zipEntry = new ZipEntry ("c:/temp/zip/1.pdf");

– where 1.pdf is the file to be compressed. ZipEntry has one of the method setMethod(). It is has the argument int, which tells how the file should be compressed. ZipEntry.STORED which tells the file entry not to be compressed. ZipEntry.DEFLATED tells the file entry to be compressed. Finally you have to read the 1.pdf and write in the zip file. Now zipfile is ready .

The following complete codes creates a file which contains the compressed files 1.pdf, 2.pdf, calc/1.pdf */

package com.javaonline;

public class ZipFileCreation {
	public static void main (String... args) throws IOException

		  // file to be created
			File testZip=new File("c:/temp/zip", "");
			FileOutputStream fileObject=new FileOutputStream(testZip) ;
			ZipOutputStream zipFile=new ZipOutputStream (fileObject);
			//1.pdf, 2.pdf and 1.pdf inside calc folder is compressed and added to
			addToZip("c:/temp/zip/1.pdf", zipFile);
			addToZip("c:/temp/zip/2.pdf", zipFile);
			addToZip("c:/temp/zip/calc/1.pdf", zipFile);


	private static void addToZip(String fileName, ZipOutputStream zipFile )
		byte[] buffer = new byte[1024];
		int len;
		ZipEntry zipEntry=null; // to add entry of file in zip fle
		FileInputStream in=null; // to read input files to be compressed
			zipEntry = new ZipEntry (fileName);
			in = new FileInputStream(fileName);
			while ((len = > 0) zipFile.write(buffer, 0, len);
	    } catch (IOException e)
		System.out.println("Exception occured " +e);



Output : file is created in the folder c:\temp\zip.
Note : Need Code to unzip the file using java? Please go through the url Unzip a zip file . You can also use winzip to un zip the file.

Reference : ZipOutputStream

You may also like

Leave a Reply

2 Comments on "Create ZIP (Compressed) file in Java with example code"

Bryan Dizon
10 months 15 days ago

It works! Thank you 🙂