Java Unzip File with Subdirectories

Example of java code to uncompress and extract files from a compressed zip file using package.

The example opens a zip file and starts traversing the files in a similar manner used in walking a directory tree. If we find a directory entry, we create a new directory. If we find a file entry, we write the decompressed file.

1. Java Unzip File Example

The Example uses ZipInputStream to read a ZipFile and then read all the ZipEntry one by one. Then uses FileOutputStream to write all the files into file system.

import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;

public class Main 
	public static void main(String[] args) 
		//Open the file 
		try(ZipFile file = new ZipFile(""))
			FileSystem fileSystem = FileSystems.getDefault();
			//Get file entries
			Enumeration<? extends ZipEntry> entries = file.entries();
			//We will unzip files in this folder
			String uncompressedDirectory = "uncompressed/";
			//Iterate over entries
			while (entries.hasMoreElements()) 
				ZipEntry entry = entries.nextElement();
				//If directory then create a new directory in uncompressed folder
				if (entry.isDirectory()) 
					System.out.println("Creating Directory:" + uncompressedDirectory + entry.getName());
					Files.createDirectories(fileSystem.getPath(uncompressedDirectory + entry.getName()));
				//Else create the file
					InputStream is = file.getInputStream(entry);
					BufferedInputStream bis = new BufferedInputStream(is);
					String uncompressedFileName = uncompressedDirectory	+ entry.getName();
					Path uncompressedFilePath = fileSystem.getPath(uncompressedFileName);
					FileOutputStream fileOutput = new FileOutputStream(uncompressedFileName);
					while (bis.available() > 0) 
					System.out.println("Written :" + entry.getName());
		catch(IOException e)

2. How Unzipping Works?

Below listed things are happening in sequence in the above example.

  1. ZipFile object represents the .zip file and is used to access its information.
  2. ZipEntry class represents an entry in the zip file – either file or directory.
  3. Each ZipEntry instance has the compressed and uncompressed size information, the name, and the input stream of the uncompressed bytes.
  4. Using InputStream and BufferedInputStream, we read the uncompressed bytes into a byte buffer to then use FileOutputStream to write it to a file.
  5. Keep doing it until whole file is processed.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

7 thoughts on “Java Unzip File with Subdirectories”

  1. This code works absolutely perfect.

    But with a huge file, this takes a lot of lots of time. Is there any way to increase the speed.

  2. Hi,

    we have a code that decompress the f zip files.

    is there anyway to catch the filenames.


    i have
    can we catch the path as \\\\1.pdf
    for above compressions

    for knowing which file belongs to which compression

  3. Hi, I am running into an issue where I have multiple levels of directories within a zip file. It seems that in some cases the code doesn’t work correctly because I am trying to unzip a file whose directory has not been created yet. The above code seems to have an expectation that the entry that represents the directory will come before any entries for files within that directory, but that does not seem to be the case. Any ideas?

  4. Hi, if i have files in side directory, how can i unzip it, can i take another while loop inside of directory or does it works with your code?


Leave a Comment


A blog about Java and related technologies, the best practices, algorithms, and interview questions.