Extract zip file with subdirectories

With this example we are going to demonstrate how to extract from a ZipFile With Subdirectories. In short, to extract from a ZipFile With Subdirectories you should:

  • Create a new File instance by the given pathname of the file.
  • Create a directory with the same name to which the contents will be extracted, using mkdir() API method of File.
  • Create a new ZipFile to read entries from the zipFile.
  • Get the Enumeration of the ZipFile entries, with entries() API method of ZipFile and iterate through each one of them.
  • For each one of them create a new File instance from the parent zip file pathname string and the specific pathname string.
  • Create parent directories, using getParentFile() method of File to get the abstract pathname of parent directory and mkdirs() API method of File that creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.
  • Check if the zip entry is a directory, with isDirectory() API method. If it is not a directory, then since it is a file extract it.
  • Create a BufferedInputStream with the input stream for reading the contents of the specified zip file entry.
  • Create a FileOutputStream to write to the file represented by the specified destination File object.
  • Create a new BufferedOutputStream to write data to the specified underlying FileOutputStream with buffer size set to 1024.
  • Read bytes from this BufferedInputStream into the specified byte array, starting at the given offset, with read(byte[] b, int off, int len) API method of BufferedInputStream and write the data to the BufferedOutputStream, with write(byte[] b, int off, int len) of BufferedOutputStream.
  • Close the ZipFile, the BufferedInputStream and the BufferedOutputStream, using their close() API methods.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.util.Enumeration;

public class ExtractZipFileWithSubdirectories {
	public static void main(String[] args) {
		String filename = "c:/";
		File srcFile = new File(filename);
		// create a directory with the same name to which the contents will be extracted
		String zipPath = filename.substring(0, filename.length()-4);
		File temp = new File(zipPath);
		ZipFile zipFile = null;
		try {
			zipFile = new ZipFile(srcFile);
			// get an enumeration of the ZIP file entries
			Enumeration e = zipFile.entries();
			while (e.hasMoreElements()) {
				ZipEntry entry = e.nextElement();
				File destinationPath = new File(zipPath, entry.getName());
				//create parent directories
				// if the entry is a file extract it
				if (entry.isDirectory()) {
				else {
					System.out.println("Extracting file: " + destinationPath);
					BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

					int b;
					byte buffer[] = new byte[1024];

					FileOutputStream fos = new FileOutputStream(destinationPath);
					BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);

					while ((b =, 0, 1024)) != -1) {
						bos.write(buffer, 0, b);
		catch (IOException ioe) {
			System.out.println("Error opening zip file" + ioe);
		 finally {
			 try {
				 if (zipFile!=null) {
			 catch (IOException ioe) {
					System.out.println("Error while closing zip file" + ioe);


This was an example of how to extract from a ZipFile With Subdirectories in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
4 years ago

I am unable to find “Enumeration” getting an error to import this class.

Valentin Loisel
2 years ago
Reply to  Vikash

Change to Enumeration e = zipFile.entries();

Last edited 2 years ago by Valentin Loisel
Riya Saha
Riya Saha
1 year ago

Here the source and destination paths are same!! Can you please tell me what to add here so that the destination path can be different!!

Back to top button