Reputation: 315
I have a zip file whose directory structure looks like this:
file.zip
- dir1
- dir1_sub_dir1
- dir1_sub_dir1_file1
- dir1_sub_dir2
- dir1_sub_dir3
- fileABC.ext
- fileDEF.ext
Is there any existing JAVA utility that can help me with the names of all the subdirectories inside a directory(of zip file). For example which takes "dir1" as input for filename "file.zip" and outputs [dir1_sub_dir1, dir1_sub_dir2, dir1_sub_dir3]. One way it can be solved is by using ZipEntry. But was exploring if this problem is already solved with some library in JAVA.
Upvotes: 0
Views: 1129
Reputation: 9192
Here are some methods I quickly put together that you may find useful for retrieving directory and or file names from within Zip files. They do make use of ZipEntry. Do as you see fit with them:
/**
* Returns a String Array of all directory paths contained within the
* supplied Zip file.<br><br>
*
* The directory names returned are full paths.<br>
*
* @param zipFilePath (String) The full Path and file name of the Zip
* file to retrieve directory paths from.<br>
*
* @return (Single Dimensional String[] Array) A String Array containing
* all the directories contained within the supplied Zip file.
*/
public static String[] getZipFile_Directories(String zipFilePath) {
java.util.zip.ZipFile zipFile = null;
try {
zipFile = new java.util.zip.ZipFile(zipFilePath);
}
catch (IOException ex) {
Logger.getLogger("getZipFile_Directories() Method Error!").log(Level.SEVERE, null, ex);
return null;
}
List<String> directories = new ArrayList<>();
java.util.Enumeration<? extends java.util.zip.ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
java.util.zip.ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
directories.add(entry.getName());
}
}
return directories.toArray(new String[directories.size()]);
}
/**
* Returns a String Array of all subdirectory paths that are contained
* within a possible directory path within the supplied Zip file.<br><br>
*
* The subdirectory names returned are full paths.<br>
*
* @param zipFilePath (String) The full Path and file name of the Zip
* file to retrieve subdirectory paths from.<br>
*
* @param fromWithinDirectory (String) The directory path to retrieve
* subdirectories from. If there is a path in the zip file that is:<pre>
*
* {@code dir/folder1/folder2/}</pre><br>
*
* and you want all the subdirectories contained within folder2 then you
* would supply:<pre>
*
* {@code dir/folder1/folder2/}</pre><br>
*
* All subdirectories of {@code dir/folder1/folder2/} (if any) will be
* returned.
*
* @return (Single Dimensional String[] Array) A String Array containing
* all the subdirectories contained within the supplied directory path
* which is also contained within the supplied Zip file.
*/
public static String[] getZipFile_SubDirectories(String zipFilePath, String fromWithinDirectory) {
java.util.zip.ZipFile zipFile = null;
try {
zipFile = new java.util.zip.ZipFile(zipFilePath);
}
catch (IOException ex) {
Logger.getLogger("getZipFile_SubDirectories() Method Error!").log(Level.SEVERE, null, ex);
return null;
}
List<String> directories = new ArrayList<>();
java.util.Enumeration<? extends java.util.zip.ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
java.util.zip.ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
String dir = entry.getName();
if (!dir.equalsIgnoreCase(fromWithinDirectory) && dir.contains(fromWithinDirectory)) {
directories.add(dir);
}
}
}
return directories.toArray(new String[directories.size()]);
}
/**
* Returns a String Array of all file paths contained within the
* supplied Zip file.<br><br>
*
* The file names returned are full paths with file name.<br>
*
* @param zipFilePath (String) The full Path and file name of the Zip
* file to retrieve file names from.<br>
*
* @return (Single Dimensional String[] Array) A String Array containing
* all the <b>path and file names</b> contained within the supplied Zip
* file.
*/
public static String[] getZipFile_Files(String zipFilePath) {
java.util.zip.ZipFile zipFile = null;
try {
zipFile = new java.util.zip.ZipFile(zipFilePath);
}
catch (IOException ex) {
Logger.getLogger("getZipFile_Files() Method Error!").log(Level.SEVERE, null, ex);
return null;
}
List<String> files = new ArrayList<>();
java.util.Enumeration<? extends java.util.zip.ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
java.util.zip.ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
files.add(entry.getName());
}
}
return files.toArray(new String[files.size()]);
}
/**
* Returns a String Array of all file paths contained within the
* supplied directory contained within the supplied Zip file.<br><br>
*
* The file names returned are full paths with file name.<br>
*
* @param zipFilePath (String) The full Path and file name of the Zip
* file to retrieve file names from.<br>
*
* @param fromWithinDirectory (String) The directory path to retrieve
* files from. If there is a path in the zip file that is:<pre>
*
* {@code dir/folder1/folder2/}</pre><br>
*
* and you want all the files contained within folder1 then you
* would supply:<pre>
*
* {@code dir/folder1/}</pre><br>
*
* All files contained within {@code dir/folder1/} (if any) will be
* returned.
*
* @return (Single Dimensional String[] Array) A String Array containing
* all the <b>path and file names</b> contained within the supplied Zip
* file.
*/
public static String[] getZipFile_FilesFromDirectory(String zipFilePath, String fromWithinDirectory) {
java.util.zip.ZipFile zipFile = null;
try {
zipFile = new java.util.zip.ZipFile(zipFilePath);
}
catch (IOException ex) {
Logger.getLogger("getZipFile_FilesFromDirectory() Method Error!").log(Level.SEVERE, null, ex);
return null;
}
List<String> files = new ArrayList<>();
java.util.Enumeration<? extends java.util.zip.ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
java.util.zip.ZipEntry entry = entries.nextElement();
String file = entry.getName();
if (!entry.isDirectory() &&
file.substring(0, file.lastIndexOf("/") + 1)
.equalsIgnoreCase(fromWithinDirectory)) {
files.add(file);
}
}
return files.toArray(new String[files.size()]);
}
Upvotes: 2