SHARE
TWEET

Untitled

a guest Jan 24th, 2020 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.apache.commons.codec.digest.DigestUtils;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. public String calcMD5HashForDir(File dirToHash, boolean includeHiddenFiles) {
  7.  
  8.     assert (dirToHash.isDirectory());
  9.     Vector<FileInputStream> fileStreams = new Vector<FileInputStream>();
  10.  
  11.     System.out.println("Found files for hashing:");
  12.     collectInputStreams(dirToHash, fileStreams, includeHiddenFiles);
  13.  
  14.     SequenceInputStream seqStream =
  15.             new SequenceInputStream(fileStreams.elements());
  16.  
  17.     try {
  18.         String md5Hash = DigestUtils.md5Hex(seqStream);
  19.         seqStream.close();
  20.         return md5Hash;
  21.     }
  22.     catch (IOException e) {
  23.         throw new RuntimeException("Error reading files to hash in "
  24.                                    + dirToHash.getAbsolutePath(), e);
  25.     }
  26.  
  27. }
  28.  
  29. private void collectInputStreams(File dir,
  30.                                  List<FileInputStream> foundStreams,
  31.                                  boolean includeHiddenFiles) {
  32.  
  33.     File[] fileList = dir.listFiles();        
  34.     Arrays.sort(fileList,               // Need in reproducible order
  35.                 new Comparator<File>() {
  36.                     public int compare(File f1, File f2) {                      
  37.                         return f1.getName().compareTo(f2.getName());
  38.                     }
  39.                 });
  40.  
  41.     for (File f : fileList) {
  42.         if (!includeHiddenFiles && f.getName().startsWith(".")) {
  43.             // Skip it
  44.         }
  45.         else if (f.isDirectory()) {
  46.             collectInputStreams(f, foundStreams, includeHiddenFiles);
  47.         }
  48.         else {
  49.             try {
  50.                 System.out.println("\t" + f.getAbsolutePath());
  51.                 foundStreams.add(new FileInputStream(f));
  52.             }
  53.             catch (FileNotFoundException e) {
  54.                 throw new AssertionError(e.getMessage()
  55.                             + ": file should never not be found!");
  56.             }
  57.         }
  58.     }
  59.  
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top