Search code examples
javajar

Count the number of files inside an entry of jar file


I'm searching for a way to count the number of files inside an entry of a jar file.

For example, assume I have inside the jar a folder called "myFolder", I want to be able to count the number of files inside of it.

I used the idea from here to get to myFolder, yet I don't know how to get to the files inside.

Here is my code:

private int countFiles() {
    JarFile jf = null;
    int counter = 0;
    try {
        jf = new JarFile(new File(ClassName.class
                .getProtectionDomain().getCodeSource().getLocation().toURI()));
        Enumeration<JarEntry> entries = jf.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                if (entry.getName().equals("myFolder/")) {
                    ///////
                }
            }
        }
    } catch (Exception ex) {
        try {
            jf.close();
        } catch (Exception e) {
        }
    }
    return counter;
}

Appreciate the help!


Solution

  • If you want to do this in Java then it might be easier to use the ZIP File System Provider from the NIO2 API.

    import java.io.IOException;
    import java.nio.file.FileSystem;
    import java.nio.file.FileSystems;
    import java.nio.file.Files;
    import java.nio.file.Path;
    
    public class Main {
    
      public static void main(String[] args) throws IOException {
        Path jarFile = Path.of("path", "to", "file.jar");
        String directoryPath = "/myFolder";
    
        long fileCount = countFilesInDir(jarFile, directoryPath);
        System.out.println(fileCount);
      }
    
      public static long countFilesInDir(Path jarFile, String directory) throws IOException {
        try (FileSystem fs = FileSystems.newFileSystem(jarFile)) {
          return Files.list(fs.getPath(directory)).count();
        }
      }
    }
    

    If you only want to count regular files then change:

    return Files.list(fs.getPath(directory)).count();
    

    To:

    return Files.list(directory).filter(Files::isRegularFile).count();
    

    And if you want to count the entries/files recursively then check out the java.nio.file.Files#find(Path,int,BiPredicate,FileVisitOption...) method.


    Note: Normally you should close the Stream returned by Files.list or Files.find when done with it, but in this case it should be closed as a consequence of the FileSystem being closed.