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 java.util.zip 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","test.zip")
FileOutputStream fileObject=new FileOutputStram(testZip) 

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

ZipOutputStream zipFile=new ZipOutputStream (fileObject);
which creates a zip file object which corresponds to file test.zip 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");
    zipFile.putNextEntry(zipEntry)

– 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 test.zip file which contains the compressed files 1.pdf, 2.pdf, calc/1.pdf */

package com.javaonline;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

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

		  //test.zip file to be created
			File testZip=new File("c:/temp/zip", "test.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 test.zip
			addToZip("c:/temp/zip/1.pdf", zipFile);
			addToZip("c:/temp/zip/2.pdf", zipFile);
			addToZip("c:/temp/zip/calc/1.pdf", zipFile);
			zipFile.close();
			fileObject.close();

	}

	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
		try
	    {
			zipEntry = new ZipEntry (fileName);
			zipEntry.setMethod(zipEntry.DEFLATED);
			zipFile.putNextEntry(zipEntry);
			in = new FileInputStream(fileName);
			while ((len = in.read(buffer)) > 0) zipFile.write(buffer, 0, len);
			zipFile.closeEntry();
			in.close();
	    } catch (IOException e)
		{
		System.out.println("Exception occured " +e);
		}

	}

}

Output :
test.zip 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