Home » Core Java » util » zip » Extract zip file with subdirectories

About Ilias Tsagklis

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

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.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ExtractZipFileWithSubdirectories {
	
	public static void main(String[] args) {
		
		String filename = "c:/archive.zip";
		
		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);
		temp.mkdir();
		
		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
				destinationPath.getParentFile().mkdirs();
				
				// if the entry is a file extract it
				if (entry.isDirectory()) {
					continue;
				}
				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 = bis.read(buffer, 0, 1024)) != -1) {
						bos.write(buffer, 0, b);
					}
					
					bos.close();
					bis.close();
					
				}
				
			}
			
		}
		catch (IOException ioe) {
			System.out.println("Error opening zip file" + ioe);
		}
		 finally {
			 try {
				 if (zipFile!=null) {
					 zipFile.close();
				 }
			 }
			 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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Vikash Recent comment authors

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

  Subscribe  
newest oldest most voted
Notify of
Vikash
Guest
Vikash

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