如何获取 zip 文件中所有目录的列表
How to get list of all the directories inside a zip file
我有一个 zip 文件,其目录结构如下所示:
file.zip
- dir1
- dir1_sub_dir1
- dir1_sub_dir1_file1
- dir1_sub_dir2
- dir1_sub_dir3
- fileABC.ext
- fileDEF.ext
是否有任何现有的 JAVA 实用程序可以帮助我获取目录(zip 文件)中所有子目录的名称。例如,将“dir1”作为文件名“file.zip”的输入并输出 [dir1_sub_dir1、dir1_sub_dir2、dir1_sub_dir3]。解决它的一种方法是使用 ZipEntry。但是正在探索这个问题是否已经用 JAVA 中的一些库解决了。
以下是我快速汇总的一些方法,您可能会发现它们对从 Zip 文件中检索目录和/或文件名很有用。他们确实使用了 ZipEntry。随心所欲:
/**
* 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()]);
}
我有一个 zip 文件,其目录结构如下所示:
file.zip
- dir1
- dir1_sub_dir1
- dir1_sub_dir1_file1
- dir1_sub_dir2
- dir1_sub_dir3
- fileABC.ext
- fileDEF.ext
是否有任何现有的 JAVA 实用程序可以帮助我获取目录(zip 文件)中所有子目录的名称。例如,将“dir1”作为文件名“file.zip”的输入并输出 [dir1_sub_dir1、dir1_sub_dir2、dir1_sub_dir3]。解决它的一种方法是使用 ZipEntry。但是正在探索这个问题是否已经用 JAVA 中的一些库解决了。
以下是我快速汇总的一些方法,您可能会发现它们对从 Zip 文件中检索目录和/或文件名很有用。他们确实使用了 ZipEntry。随心所欲:
/**
* 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()]);
}