Java如何从压缩文件中获取顶层文件夹

3

我是一名有用的助手,可以为您进行文本翻译。

我被卡在这个问题上了。我想要从zip文件中只打印顶层目录。例如,我有一个具有以下结构的zip文件:

Sample.zip
    - sound
          - game
                -start.wav
                -end.wav
          - Intro
    - custom
          - Scene
                - fight
          - Angle
       ..............

上图显示:Sample.zip有2个文件夹(sound和custom),在sound文件夹里有2个文件夹game和Intro等等...
现在我知道如何打开和获取zip文件中的目录:例如(工作代码)
try {
    appFile = ("../../Sample.zip"); // just the path to zip file
    ZipFile zipFile = new ZipFile(appFile);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if(entry.isDirectory()){
            String dir = entry.getName();
            File file = new File(dir);
            System.out.println(file.getParent());
        }
    }
} catch (IOException e) {
    System.out.println("Error opening Zip" +e);
}

现在我也知道可以使用.getParent()(如上所示)来获取顶级目录,但是上述实现没有生效。它会列出所有的目录,例如:
 null   
 sound
 game
 null
 custom
 scene
 Angle 

我的问题是如何只打印顶级文件夹,在上述情况下,即soundcustom
如果有任何提示,我将不胜感激。

@Ducan:我已经更新了问题,并附上了未能解决的部分(输出)。 - Nepal12
首先打印所有条目的名称。然后只打印目录条目的名称。现在阅读输出并尝试找出如何区分顶级目录条目和非顶级目录条目。 - JB Nizet
6个回答

3

实际上,我按照@JB Nizet的建议进行了以下操作,并找到了一个解决方法(它确实有效):

try {
    appFile = ("../../Sample.zip"); // just the path to zip file
    ZipFile zipFile = new ZipFile(appFile);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if(entry.isDirectory()){
            File file = new File(entry.getName());
            if(file.getParent() == null){
               System.out.println(file.getName());
            }
        }
    }
} catch (IOException e) {
    System.out.println("Error opening Zip" +e);
}

上述解决方案有效是因为顶级目录没有父级,因此返回null作为输出。所以我只需循环遍历目录,查看它们是否有父级,如果它们没有任何父级,则它们是顶级目录。

那是错误的。它仅仅能运行是因为你在当前目录下没有名为'sound'或'game'的目录。你不需要使用File,只需从名称中删除尾随的'/'并查看结果名称是否仍包含'/'。如果没有,那就是顶层目录。 - JB Nizet
@JBNizet :抱歉,我没听懂你的逻辑。你能否举个小例子来说明一下? - Nepal12
boolean isTopLevelDirectory(ZipEntry e) { return e.isDirectory() && !e.getName().substring(0, e.getName().length() - 1).contains("/"); } - JB Nizet
@JBNizet:谢谢,我现在明白了,而且它正在工作。你能否将其发布为答案,以便我可以接受并标记此问题已得到解答。 - Nepal12

2
你可以使用类似这样的内容:
    try{
        String appFile = "../file.zip"; // just the path to zip file
        ZipFile zipFile = new ZipFile(appFile);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if(entry.isDirectory() && !entry.getName().matches("\\S+/\\S+")){ //it's a top level folder
                System.out.println(entry.getName());
            }
        }
    } catch (IOException e) {
        System.out.println("Error opening Zip" +e);
    }

抱歉,但您的解决方案将列出所有目录。 - Nepal12
很奇怪,因为从entry.getName()中得到的目录“sound/game/”在名称中包含一个/,而它应该被正则表达式条件排除。 - Le Funes
是的,我认为你的答案应该有效。不过我已经解决了,但是我所做的只是一个权宜之计。 - Nepal12

1
以下是一种方法:
/**
 * Get the root folders within a zip file
 *
 * @param zipFile the zip file to be used. E.g. '/home/foo/bar.zip'
 * @return a list containing all root folders
 * @throws Exception if case the zip file cannot be found or read.
 */
public static List<String> getGetRootDirectoriesWithinZip(String zipFile) throws Exception {
    Set<String> set = new LinkedHashSet();
    //get the zip file content stream
    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile));

    //get the zipped file set entry
    ZipEntry zipEntry = zipInputStream.getNextEntry();
    while (zipEntry != null) {

        String fileName = zipEntry.getName();
        Path path = Paths.get(fileName);
        int nameCount = path.getNameCount();
        for (int i = 0; i < nameCount; i++) {
            if (path != null && path.getParent() != null) {
                path = path.getParent();
            }

        }
        set.add(path.toString());
        zipEntry = zipInputStream.getNextEntry();
    }

    List<String> retList = new ArrayList<>();
    retList.addAll(set);
    return retList;
}

1
也许这段代码能帮助你使用InputStream。
            String topFolder="";
            String topFolder2="";
            Boolean hasTopFolder=true;
            try{
            File dir = new   File(path+"/html5"+catalogue.getIdProduit());
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String outputFolder= "path/to/outputFolder";       
            InputStream input = file.getInputstream();

            //get the zip file content
            ZipInputStream zis = new ZipInputStream(input);
            //get the zipped file list entry

            ZipEntry ze = zis.getNextEntry();

            while(ze!=null){

                if (ze.isDirectory()) {
                    System.out.println("is directory : "+ ze.getName());
                    if ("".equals(topFolder)){
                        topFolder = ze.getName().split("/")[0];                   
                        System.out.println("is directory topFolder : "+ ze.getName());

                    }

                    if (("".equals(topFolder2)) && (!topFolder.equals(ze.getName().split("/")[0]))){
                        hasTopFolder=false;
                        topFolder2=ze.getName().split("/")[0];
                        System.out.println("is directory topFolder2 : "+ ze.getName());
                    }
                    ze = zis.getNextEntry();              
                    continue;

                }

                String fileName = ze.getName();
                File newFile = new File(outputFolder + File.separator + fileName);

                System.out.println("file unzip : "+ newFile.getAbsoluteFile());

                 //create all non exists folders
                 //else you will hit FileNotFoundException for compressed folder
                 new File(newFile.getParent()).mkdirs();

                 FileOutputStream fos = new FileOutputStream(newFile);             

                 int len;
                 while ((len = zis.read(buffer)) > 0) {
                     fos.write(buffer, 0, len);
                 }

                 fos.close();   
                 ze = zis.getNextEntry();
                 }
                 zis.closeEntry();
                 zis.close();
                 System.out.println("Done");
            }
            catch(IOException e){
                e.printStackTrace();
            }



            if (hasTopFolder){
                topFolder="/"+topFolder;
            }
            else 
                topFolder="";

0

这是对我有效的方法。

我应该指出,我正在使用StringUtils(Apache Lang3)来计算ZipEntry路径中“\”出现的次数,虽然如果您不想使用StringUtils,也可以编写自己的计数方法。

public static ArrayList<ZipEntry> getZipContent(File file, int index) {
    try {
        ArrayList<String> innerFoldersPaths = new ArrayList<String>();
        ArrayList<ZipEntry> retEntries = new ArrayList<ZipEntry>();
        ZipFile zipFile = new ZipFile(file);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            // If you also want to get files remove the "if (entry.isDirectory())" statement.
            if (entry.isDirectory()) {
                String backSlashName = entry.getName().replace("/", "\\"); // Important to do this.
                if (StringUtils.countMatches(backSlashName, "\\") > index - 1) { // Using Apache StringUtils
                    String folder[] = backSlashName.split(Pattern.quote("\\"));
                    String finalFolder = "";
                    // Getting the folders path inside the .zip file .
                    for (int i = 0; i < index; i++) {
                        folder[i] = folder[i] + "\\";
                        finalFolder = finalFolder + folder[i];
                    }
                    finalFolder = finalFolder.replace("\\", "/"); // Important to do this.
                    if (innerFoldersPaths.contains(finalFolder)) {
                    } else {
                        innerFoldersPaths.add(finalFolder);
                    }
                }
            }
        }
        for (String backSlashName : innerFoldersPaths) {
            retEntries.add(zipFile.getEntry(backSlashName));
        }
        zipFile.close();
        return retEntries;
    } catch (Exception exception) {
        // handle the exception in the way you want.
        exception.printStackTrace();
    }
    return null;
}

这个方法的用法是:
    File file = new File("Your zip file here");
    for (ZipEntry zipEntry : getZipContent(file, 1)) { // This would return all the folders in the first file
        // Do what ever your wantt with the ZipEntry
        System.out.println(zipEntry.getName());
    }

如果您想获取除第一个文件夹以外的所有文件夹, 您可以通过更改索引来获取所需深度的文件夹。

-2
请使用 Kotlin 中的此函数。
fun getRootFolderName(fileAddress: String): String {
    if (File(fileAddress).parent == null || ("" + File(fileAddress).parent).length < 1) return File(fileAddress).name
    return getRootFolderName("" + File(fileAddress).parent)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接