Maciek Nicewicz
Maciek Nicewicz

Reputation: 61

Java code - number of files in a JAR

I want to find the number of files the JAR contains. Is there a way to do it without looping through like this:

while (enumEntries.hasMoreElements()) {
    enumEntries.nextElement();
    count++;
}

I want to have the number of files before I loop through the entries. It seems not elegant to loop through them twice.

Upvotes: 2

Views: 98

Answers (2)

ashiquzzaman33
ashiquzzaman33

Reputation: 5741

You can use size() to count the number of entries that's what your loop return.

Uses

JarFile f = new JarFile(new File("path.jar"));
System.out.println(f.size());

Upvotes: 1

André Schild
André Schild

Reputation: 4754

JAR files are basically zip files.

So you can use the jar java class to retrieve the infos from it, like in this post.

Be aware that the size() method returns the number of entries, which can also include folders and not only files.

Upvotes: 0

Related Questions