Question

I would like to return values of a recursive method which list all files in directory and subdirectories. The goal is to create a HashSet of md5 file values.

For the moment the code works fine but just in root dir, not in recursive.

static Set<String> localMd5Listing() throws Exception {

        List<String> localMd5List = new ArrayList<String>();

        if(!baseModDirectoryFile.exists()){
            System.out.println("baseModDirectory doesn't exist, check your baseModDirectory variable.");
        }
        else{
            if(baseModDirectoryFile.isDirectory()){
                File[] paths = baseModDirectoryFile.listFiles();

                if(paths != null){
                    for(File path:paths){
                        if(path.isFile()){
                            FileInputStream fis = new FileInputStream(path);
                            String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis);
                            fis.close();
                            localMd5List.add(md5);
                        }
                        else if(path.isDirectory()){
                            listChildren(path);
                            //Check md5 for children files as path.isFile condition
                        }

                    }

                }


            }


        }

        Set<String> localSet = new HashSet<String>();
        localSet.addAll(localMd5List);
        localMd5List.clear();
        localMd5List.addAll(localSet);

        return localSet;
    }

listChildren method for recursive result :

public static void listChildren(File dir) throws IOException{
        File[] files = dir.listFiles();
        for(File file:files){
            if(file.isDirectory()){
                listChildren(file);
            }
            else{
                //Return file variable to use them in localMd5Listing()
            }
        }

    }

Unfortuntely I didn't know how to link the 2 methods to return values of listChildren() in localMd5Listing(). I think it's not the good way to have a listFile() in the first method too.

Thank you !

Was it helpful?

Solution

Extract the if statement in localMD5listing into a method recurseMD5 that takes a File argument and the list of hashes for update. Then start off the process by calling

recurseMD5(baseModDirectoryFile, localmd5List);

and in recurseMD5 you just recurse on all listFiles() when the parameter is a directory. If, OTOH, it is a regular file, you add the md5.

void recurseMD5(File it, List<String> hashes) {
     if (it.isDirectory) {
        for (File f : it.listFiles()) recurseMD5(f, hahses);
     }
     else {
         // process MD5 hash of file
     }
}

OTHER TIPS

the basic setup you want is something like the following pseudocode

public List<string> getAllHashes() {
    List<String> hashes = new List<String>();
    //calculate all the hashes
    foreach(directory d in directories) {
        hashes.add(directory.getAllHashes());
    }
    return hashes;
}

I know that this is no full code whatsoever, but with this you should be able to make the recursive loop. Don't forget to check if there actually ARE directories in there, or you might get a nullpointer!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top