Java iterations through all files and only unique RAR archives in the directory

How can I iterate over all RAR archives in a directory. I know how to iterate over files in a directory, but I'm only interested in RAR archives. If they were simple RAR archives with the extension .rar, this would not be a problem, but my directories could have several pending archives, and I only need the first / main volume in the set. I would also like to have other files in the directory:

Here is the contents of the examples directory:

  • file.txt
  • somefile.zip
  • hello.rar
  • test.part1.rar
  • test.part2.rar
  • example.rar
  • example.r00

Result:

  • file.txt
  • somefile.zip
  • hello.rar
  • test.part1.rar
  • example.rar

This is what I use ti to iterate through the directory:

  import java.io.File;

  ...

  for (File child : (new File(myDirectoryPath)).listFiles()) {
    if (!child.isDirectory()) {
      //Do something with the file
    }
  }

How can i do this? I need to determine if this is a RAR archive or not. If not, use it. If so, I will need to check if this is the first part of the archive. if so, do something, otherwise ignore it.

thank

+1
4

, RAR-, .

/**
 * Checks whether a file is an archive
 *
 * @param    filFile        the file to checks
 * @retuns                  a bollean value indicating the result
 */
 public static Boolean isArchive(File filFile) {  

     try {

         byte[] bytSignature = new byte[] {0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00};
         FileInputStream fisFileInputStream = new FileInputStream(filFile);

         byte[] bytHeader = new byte[20];
         fisFileInputStream.read(bytHeader);

         Short shoFlags = (short) (((bytHeader[10]&0xFF)<<8) | (bytHeader[11]&0xFF));

         //Check if is an archive
         if (Arrays.equals(Arrays.copyOfRange(bytHeader, 0, 7), bytSignature)) {
             //Check if is a spanned archive
             if ((shoFlags & 0x0100) != 0) {
                 //Check if it the first part of a spanned archive
                 if ((shoFlags & 0x0001) != 0) {
                     return true;
                 } else {
                     return false;
                 }
             } else {
                 return true;
             }
         } else {
             return true;
         }

     } catch (Exception e) {
         return false;
     }

 }

RAR . , :

, ?.

+2

, , . , , , rar. - , rar . , .

file.rar file.rXX ( 2 ) , file.partXXX.rar, file.part1.rar (XXX 1 - ). , FilenameFilter - .

, , , , somefile.part2.rar otherfile.r03, , , .

for (File child : (new File(myDirectoryPath)).listFiles(new FilenameFilter() {
    private Pattern p1 = null;
    private Pattern p2 = null;
    public boolean accept(File dir, String name) {

        name = name.toLowerCase();

        if(p1 == null) {
            p1 = Pattern.compile("\\.r\\d\\d");
            p2 = Pattern.compile("\\.part\\d+\\.rar");
        }

        if(name.endsWith(".part1.rar")) {
            return true;
        }
        else if(p2.matcher(name).matches()) {
            return false;
        }
        else {
            return !p1.matcher(name).matches();
        }
    }
}) {
    if (!child.isDirectory()) {
      //Do something with the file
    }
}
+2

, ".rar" ) ,

Set<String> fileSet=new HashSet<String>();

if(fileName.endsWith(".rar")){
    set.add(fileName);
}
0

Step 1: File.listFile(FileFilter)- your friend. Implemented correctly, giving you only RAR and numbered files.

Step 2: Collect the prefixes of all the names in different collections, as suggested above.

0
source

Source: https://habr.com/ru/post/1731685/


All Articles