How to Decompress/Unzip a Zip File in Java

0 Flares 0 Flares ×

As of now we have learnt how to compress files in Java using the “java.util.zip” package. Now in this Java tutorial we are going to lean how to decompress or unzip a zip file in Java. The sample Java program will be capable of unzipping a zip file.

The Concept

The concept is very simple and followed as below.

  1. Read the zip file with ZipInputStream
  2. Get the files into ZipEntry
  3. Create the target folder if does not exist
  4. Output the files to FileOutputStream

Example Program to Unzip a Zip File in Java

package com.lessonslab.util.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnzipAZipFileInJava
{
List<String> fileList;
private static final String SOURCE_ZIP_FILE = "/home/hduser/test/myfolder.zip";
private static final String TARGET_FOLDER = "/home/hduser/test/MyUnzipFolder";

UnzipAZipFileInJava()
{
fileList = new ArrayList<String>();
}

public static void main(String args[])
{
UnzipAZipFileInJava unzipApp = new UnzipAZipFileInJava();
unzipApp.unZipMe(SOURCE_ZIP_FILE,TARGET_FOLDER);
}

private void unZipMe(String zipFile, String targetFolder)
{
byte[] buffer = new byte[1024];
try
{
//First of all we need to create the target folder if it does not exist
File opFolder = new File(targetFolder);
if(!opFolder.exists())
{
opFolder.mkdir();
}
System.out.println("Process Started.....");
//Get hold of the ZipInputStream from the zip file
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
//Get hold of the zip file list entries
ZipEntry zipEntry = zis.getNextEntry();
while(zipEntry != null)
{
//Get the name of the file
String fileName = zipEntry.getName();
File tempFile = new File(targetFolder + File.separator + fileName);
System.out.println("Unzipping: " + tempFile.getName());
//Get the parent folder of the temp file and create the folder
new File(tempFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(tempFile);

int len;
while ((len = zis.read(buffer)) > 0)
{
fos.write(buffer, 0, len);
}
fos.close();
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
System.out.println("Process completed successfully");
}
catch(IOException ex)
{
System.out.println("Unable to process..." + ex.getMessage());
}
}
}

Output

Process Started.....
Unzipping: TestDoc-2
Unzipping: TestDoc-4
Unzipping: TestDoc-3
Unzipping: TestDoc-5
Unzipping: TestDoc-1
Unzipping: TestDoc-2
Unzipping: TestDoc-4
Unzipping: TestDoc-3
Unzipping: TestDoc-2
Unzipping: TestDoc-4
Unzipping: TestDoc-3
Unzipping: TestDoc-5
Unzipping: TestDoc-1
Unzipping: TestDoc-5
Unzipping: TestDoc-1
Unzipping: TestDoc-2
Unzipping: TestDoc-4
Unzipping: TestDoc-3
Unzipping: TestDoc-5
Unzipping: TestDoc-1
Process completed successfully

You can use the above program as a base to create other unzip applications of your own.

Guru

+Ayodhyanath Guru holds a B.Tech degree in Electrical Engineering and has worked with various prestigious clients in the IT industry and presently working as a Software Engineer. He is a part time blogger and presently authors the Jafaloo.Com blog. Being a tech enthusiast Guru likes to surf the web and blogs about interesting technical topics like How-To guides, freewares, Tutorials, Software, Gadgets, web applications etc. Apart from blogging he likes coding in Java/J2EE and PHP.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 0 Flares ×