views:

377

answers:

2

I am looking to use java or groovy to get the md5 checksum of a complete directory.

I have to copy directories for source to target, checksum source and target, and after delete source directories.

I find this script for files, but how to do the same thing with directories ?

import java.security.MessageDigest

    def generateMD5(final file) {
       MessageDigest digest = MessageDigest.getInstance("MD5")
       file.withInputStream(){is->
       byte[] buffer = new byte[8192]
       int read = 0
          while( (read = is.read(buffer)) > 0) {
                 digest.update(buffer, 0, read);
             }
         }
       byte[] md5sum = digest.digest()
       BigInteger bigInt = new BigInteger(1, md5sum)
       return bigInt.toString(16).padLeft(32, '0')
    }

Is there a better approach ?

A: 

It's not clear what it means to take the md5sum of a directory. You might want the checksum of the file listing; you might want the checksum of the file listings and their contents. If you're already summing the file data themselves, I'd suggest you spec an unambiguous representation for a directory listing (watch out for evil characters in filenames), then compute and hash that each time. You also need to consider how you will handle special files (sockets, pipes, devices and symlinks in the unix world; NTFS has file streams and I believe something akin to symlinks as well).

crazyscot
+1  A: 

I made a function to calculate MD5 checksum on Directory :

First, I'm using FastMD5: http://www.twmacinta.com/myjava/fast_md5.php

Here is my code :

  def MD5HashDirectory(String fileDir) {
    MD5 md5 = new MD5();
    new File(fileDir).eachFileRecurse{ file ->
      if (file.isFile()) {
        String hashFile = MD5.asHex(MD5.getHash(new File(file.path)));
        md5.Update(hashFile, null);
      }

    }
    String hashFolder = md5.asHex();
    return hashFolder
  }
Fabien Barbier
With groovy (probably also in Java), it can be useful to use Ant (or better Gant). See : http://ant.apache.org/manual/dirtasks.html
Fabien Barbier