java—如何获取zip文件中所有目录的列表

cwdobuhd  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(1034)

我有一个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\u sub\u dir1,dir1\u sub\u dir2,dir1\u sub\u dir3]。解决这个问题的一个方法是使用zipentry。但是我们正在探索这个问题是否已经用java中的某个库解决了。

xv8emn3q

xv8emn3q1#

下面是我快速组合的一些方法,您可能会发现这些方法对于从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()]);
}

相关问题