How to unzip files from zip file by java?

In Java package provide below classes and api’s to convert files to zip file.

ZipInputStream: This class implements an input stream filter for reading files in the ZIP file format.

ZipEntry: Class is used for zip entry.

In below example will show to unzip files from a zip file  and store in folder



public class TestUnzip {

	public static void main(String[] args) {
		TestUnzip test = new TestUnzip();
		test.unzipFiles("C:\\", "C:\\output");


	public void unzipFiles(String sourceZip, String outputLocation) {
		File outputFolder = null;
		ZipInputStream zis = null;
		ZipEntry ze = null;
		String zipFileName = null;
		File unzipFileName = null;
		FileOutputStream fos = null;
		try {
			// Create Output folder if not exist
			outputFolder = new File(outputLocation);
			if (!outputFolder.exists()) {
		} catch (Exception ex) {
		// Unzip files to output folder
		if (outputFolder != null) {

			byte[] buffer = new byte[1024];

			try {
				// Create Input Stream to read zip file
				zis = new ZipInputStream(new FileInputStream(sourceZip));
				// Get entry of next available zip file
				ze = zis.getNextEntry();

				while (ze != null) {

					zipFileName = ze.getName();
					// Path to file where unzip file will store
					unzipFileName = new File(outputFolder + File.separator + zipFileName);

					System.out.println("Unzip File : " + unzipFileName.getAbsoluteFile());
					// Create directory structure if not exist
					new File(unzipFileName.getParent()).mkdirs();
					// Create unzip file on this directory
					fos = new FileOutputStream(unzipFileName);
                    //Read zip file data in Buffer and write to file
					int len;
					while ((len = > 0) {
						fos.write(buffer, 0, len);

					ze = zis.getNextEntry();

				System.out.println("Unzip Done for all files");
			} catch (IOException ex) {
			} finally {
				try {
					if (zis != null) {
						// Close current entry of file for reading
						// close Zip and realease resources associated to it
				} catch (IOException ex) {



More Sample Code

For more java and JDBC codes follow below links


About Saurabh Gupta

My Name is Saurabh Gupta, I have approx. 11 Year of experience in Information Technology World manly in Java/J2EE. During this time I have worked with multiple organization with different client, so many technology, frameworks etc.
This entry was posted in Java and tagged , , , , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s