• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

How to best get filecount of a rar compressed file

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Saloon Keeper
Posts: 9348
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A matter of style here:  This code

can be shortened to

or perhaps even better
 
Knute Snortum
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic