zip

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.

Ilias Tsagklis

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

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

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Vikash
Vikash
5 years ago

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

Valentin Loisel
3 years ago
Reply to  Vikash

Change to Enumeration e = zipFile.entries();

Last edited 3 years ago by Valentin Loisel
Riya Saha
Riya Saha
2 years 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