• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to best get filecount of a rar compressed file  RSS feed

 
Max Winfield
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RAREntry has all these fields, surely 'directory' is the one you're looking for.


P.S.
A legitimate file can be zero bytes in length.
 
Max Winfield
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANK YOU Carey Brown for pointing out the .isDirectory() method. I was so fixated on getting the filecount that i didnt think about the reverse way to check for Directory.

Here a solution how to count files in a rar and put it in a int variable.

[Point 4 at the end has the usable code]

1. Go to https://github.com/radarsh/raroscope and get RARFile.java and RAREntry.java
2. Add them to your Project in the same package in which the class is that does the counting
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.

 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A matter of style here:  This code

can be shortened to

or perhaps even better
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formatting of your code is poor.  This is considered a bug by many, myself included.  See how much clearer your code is properly formatted:

I also used the try/resources form of try catch so you don't need to explicitly close the RARFile.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!