Java从zip文件中获取顶级文件夹

时间:2015-03-27 14:46:26

标签: java file zip directory

我有点陷入这个问题。我想从zip文件中只打印顶级目录。例如,我有一个具有以下结构的zip文件:

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

上图显示:Sample.zip有2个文件夹(声音和自定义),内部声音有2个文件夹游戏和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

对于任何暗示,我都会感激不尽。

5 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分:2)

实际上我按照@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作为输出。所以我只是循环遍历目录,看看他们是否有父母,如果他们没有父母那么他们就是顶级目录。

答案 2 :(得分: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="";

答案 3 :(得分:0)

以下方法怎么样:

/**
 * 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;
}

答案 4 :(得分:0)

这是对我有用的方法。

我应该注意,我正在使用StringUtilsApache 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());
    }

如果要获取第一个文件夹之后的所有文件夹, 您可以通过将索引更改为要获取的文件夹的深度来做到这一点。