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.
- Read the zip file with ZipInputStream
- Get the files into ZipEntry
- Create the target folder if does not exist
- 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());
}
}
}
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.
The post How to Decompress/Unzip a Zip File in Java appeared first on Jafaloo - The Tech Blog.