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

Posted

技术标签:

【中文标题】如何获取 zip 文件中所有目录的列表【英文标题】:How to get list of all the directories inside a zip file 【发布时间】:2020-12-06 05:38:59 【问题描述】:

我有一个 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 中的某些库得到解决。

【问题讨论】:

为什么你认为你需要一个替代ZipEntry的方法? @Abra 不是替代方案,而是使用 ZipEntry 逻辑再次实现它,如果某些库已经提供了此功能 嗯,有 Apache 的通用压缩库供人们使用 ZipEntry 以外的功能。但是,嗯,它不能满足您的需求,因为 ZipEntry 可以满足您的需求。 new ZipFile(fileName).stream().filter(ZipEntry::isDirectory).map(ZipEntry::getName).filter(n -> n.startsWith("dir1/"))足够了吗? 【参考方案1】:

以下是我快速汇总的一些方法,您可能会发现它们对于从 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 文件中所有目录的列表的主要内容,如果未能解决你的问题,请参考以下文章

如何读取多个 zip 文件中的所有 csv 文件?

Vue.js webpack:如何获取目录中的文件列表?

如何获取子目录中所有文件的创建日期[重复]

python:获取目录中所有* .txt文件的列表[重复]

如何获取 iphone 中下载的 zip 文件名

如何在 Python 中使用 os.walk 获取特定文件或目录列表?