Java Unzip File with Subdirectories

Example of java code to uncompress and extract files from a compressed zip file using java.util.zip 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.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main 
{
	public static void main(String[] args) 
	{
		//Open the file 
		try(ZipFile file = new ZipFile("files.zip"))
		{
			FileSystem fileSystem = FileSystems.getDefault();
			//Get file entries
			Enumeration<? extends ZipEntry> entries = file.entries();
			
			//We will unzip files in this folder
			String uncompressedDirectory = "uncompressed/";
			Files.createDirectory(fileSystem.getPath(uncompressedDirectory));
			
			//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
				else 
				{
					InputStream is = file.getInputStream(entry);
					BufferedInputStream bis = new BufferedInputStream(is);
					String uncompressedFileName = uncompressedDirectory	+ entry.getName();
					Path uncompressedFilePath = fileSystem.getPath(uncompressedFileName);
					Files.createFile(uncompressedFilePath);
					FileOutputStream fileOutput = new FileOutputStream(uncompressedFileName);
					while (bis.available() > 0) 
					{
						fileOutput.write(bis.read());
					}
					fileOutput.close();
					System.out.println("Written :" + entry.getName());
				}
			}
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
}

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+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

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.

    Reply
  2. Hi,

    we have a code that decompress the f zip files.

    is there anyway to catch the filenames.

    example:

    i have 1.zip
    2.zip
    3.zip
    1.pdf
    4.zip
    1.txt
    can we catch the path as \1.zip\2.zip\3.zip\1.pdf
    \1.zip\2.zip\4.zip\1.txt
    for above compressions

    for knowing which file belongs to which compression

    Reply
  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?

    Reply
  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?

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.