Here a solution how to count files in a rar. [Emptyfiles (filesize = 0 Bytes) are not considered files in this solution and will not be counted]
[Point 8 at the end has the usable code]
1. Got at
https://github.com/radarsh/raroscope RARFile.java and RAREntry.java and i think import java.io.IOException; import java.util.Enumeration; are needed as well.
2. Added them to my Project
3. Their Code Sample
// Construct the RARFile object using the file path
RARFile file = new RARFile("D:/Data.rar");
// Get the handle to the Enumeration
Enumeration<RAREntry> entries = file.entries();
// Iterate and print
while (entries.hasMoreElements()) {
RAREntry entry = entries.nextElement();
System.out.println(entry.getName());
}
4. What i did was instead of entry.getName()); i used entry.getSize() and you get, if several files are in a folder, a long value with the bytesize for every single file contained.
5. The Problem was that if the rar contains folders those folder would be given out as well with value 0.
Example Output of a rar file:
0
2324866
2044679
2268156
As you can see 3 files and 1 Folder (Size 0)
6. i wrote to check in between the loop which gets the values of each file to check if long is != 0 and then count with a counting variable:
long n = entry.getSize();
if (n != 0) {m++;}
System.out.println(m);
7. This gives you the filecount (empty files are not considered though, which in my case isnt important)!
8. Final Code Example: only "c:/test.rar" in line 1 must be set to desired .rar or file array containing rar File. Variable m gives you the file count. Dont forget to have the 2 raroscape classes in your project though, because they are needed. (See point 1 further up)