In this tutorial, we are going to learn how we can unzip all the zip files present in the folder and sub-folders.
Steps to follow:
- loop through the folder and subfolders and get the files inside it.
- Check if the file is a zip file or not
- If the file is a zip file then extract that file in the corresponding folder
Create a java class called UnzipFolderFile.java. First, let's implement the method which will loop through the folder and subfolders and read the zip file.
public static void searchZipFile(String directoryName) throws IOException {
File directory = new File(directoryName);
File[] fList = directory.listFiles();
if (fList != null)
for (File file : fList) {
if (file.isDirectory()) {
searchZipFile(file.getAbsolutePath());
} else if (file.isFile()) {
String ext = getFileExtension(file.getName());
if (ext.equals(".zip")) {
System.out.println("Zip file found");
String zipFilePath = file.getAbsolutePath();
String unZipOutputPath = getFileNameWithoutExtension(zipFilePath); // using apache commons i.o library
unzipFile(zipFilePath, unZipOutputPath);
searchZipFile(unZipOutputPath); // for nested zip file
}
}
}
}
Here, we are using the recursion in order to search zip files inside sub-folders. If the current file is a directory then it will again search inside this directory. If it is a zip file then it will unzip that file.
Note that we need to extract the nested zip file i.e zip file inside zip file case so, we used searchZipFile(unZipOutputPath); recursion.
In order to get the current folder, we are using the getFileNameWithoutExtension method which we gonna implement later. Now, let's implement the unzipFile method getFileExtension to get the extension of the file in order to test the current file is a zip file or not
public static String getFileExtension(String fileName) {
int lastIndexOf = fileName.lastIndexOf(".");
if (lastIndexOf == -1) {
return ""; // empty extension
}
return fileName.substring(lastIndexOf).toLowerCase();
}
public static void unzipFile(String zipFilePath, String outputDir) throws IOException {
byte[] buffer = new byte[1024];
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
File newFile = new File(outputDir + File.separator, zipEntry.getName());
if (zipEntry.isDirectory()) {
if (!newFile.isDirectory() && !newFile.mkdirs()) {
throw new IOException("Failed to create directory " + newFile);
}
} else {
File parent = newFile.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("Failed to create directory " + parent);
}
FileOutputStream fos = new FileOutputStream(newFile);
int len = 0;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
}
This method accepts the zip file path and output path to unzip and extract the zip file and output it in the output directory provided.
Now, create getFileNameWithoutExtension method to get the directory path. For this, we are using org.apache.commons.io library. For this, load the jar file in your IDE, or if you have existing projects like Maven or Gradle add the dependency as below.
For Gradle:
Add in build.gradle under "dependencies"
implementation 'org.apache.directory.studio:org.apache.commons.io:2.4'
For Maven:
Add in pom.xml
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.io</artifactId>
<version>2.4</version>
</dependency>
public static String getFileNameWithoutExtension(String name) {
return FilenameUtils.removeExtension(name);
}
Now, implement the main method to execute the program
public static void main(String[] args) {
String directory = "path-to-folder";
try {
searchZipFile(directory);
} catch (IOException e) {
e.printStackTrace();
}
}
The overall implementation looks as below:
package io;
import org.apache.commons.io.FilenameUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class UnzipFolderFile {
public static void main(String[] args) {
String directory = "path-to-folder";
try {
searchZipFile(directory);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void searchZipFile(String directoryName) throws IOException {
File directory = new File(directoryName);
File[] fList = directory.listFiles();
if (fList != null)
for (File file : fList) {
if (file.isDirectory()) {
searchZipFile(file.getAbsolutePath());
} else if (file.isFile()) {
String ext = getFileExtension(file.getName());
if (ext.equals(".zip")) {
System.out.println("Zip file found");
String zipFilePath = file.getAbsolutePath();
String unZipOutputPath = getFileNameWithoutExtension(zipFilePath); // using apache commons i.o library
unzipFile(zipFilePath, unZipOutputPath);
searchZipFile(unZipOutputPath); // for nested zip file
}
}
}
}
public static void unzipFile(String zipFilePath, String outputDir) throws IOException {
byte[] buffer = new byte[1024];
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
File newFile = new File(outputDir + File.separator, zipEntry.getName());
if (zipEntry.isDirectory()) {
if (!newFile.isDirectory() && !newFile.mkdirs()) {
throw new IOException("Failed to create directory " + newFile);
}
} else {
File parent = newFile.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("Failed to create directory " + parent);
}
FileOutputStream fos = new FileOutputStream(newFile);
int len = 0;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
}
public static String getFileExtension(String fileName) {
int lastIndexOf = fileName.lastIndexOf(".");
if (lastIndexOf == -1) {
return ""; // empty extension
}
return fileName.substring(lastIndexOf).toLowerCase();
}
public static String getFileNameWithoutExtension(String name) {
return FilenameUtils.removeExtension(name);
}
}
0 comments:
New comments are not allowed.