2013-11-29 55 views
0

我想獲得文件目錄的MD5校驗和,我已經得到了文件的算法,但是當我將它用於目錄時,結果爲空。我怎樣才能快速獲得校驗和。 以下是我對一個文件的解析(從匿名stackoverflower編輯)。如何在Android/Java中獲取文件目錄的MD5校驗和

public String fileToMD5(String filePath) { 
     InputStream inputStream = null; 
     try { 
      inputStream = new FileInputStream(filePath); // Create an FileInputStream instance according to the filepath 
      byte[] buffer = new byte[1024]; // The buffer to read the file 
      MessageDigest digest = MessageDigest.getInstance("MD5"); // Get a MD5 instance 
      int numRead = 0; // Record how many bytes have been read 
      while (numRead != -1) { 
       numRead = inputStream.read(buffer); 
       if (numRead > 0) 
        digest.update(buffer, 0, numRead); // Update the digest 
      } 
      byte [] md5Bytes = digest.digest(); // Complete the hash computing 
      return convertHashToString(md5Bytes); // Call the function to convert to hex digits 
     } catch (Exception e) { 
      return null; 
     } finally { 
      if (inputStream != null) { 
       try { 
        inputStream.close(); // Close the InputStream 
       } catch (Exception e) { } 
      } 
     } 
    } 

我搜索,有一些solutings:該目錄下

  1. 預購文件。
  2. 將目錄壓縮到諸如.zip或.rar之類的存檔文件中,並對其進行校驗。
  3. 將目錄下的所有內容轉換爲流,並進行校驗和。

我不知道是否有一些方便的解決方案。提前感謝您。

回答

0
它會改變

據我所知,沒有一種有效的方法來獲取目錄的校驗和。

1

我只是做了這一點,但我這麼做是爲了我知道將是平的(無子目錄)

  • 迭代通過目錄中的文件
  • 獲取每個文件
  • 追加的MD5目錄該MD5的字符串
  • 我做通過文件迭代後,我得到的字符串的MD5所包含的所有其他文件MD5s

這會影響到如果有任何文件改變,這個「目錄」md5也會改變。

我知道還有其他的方式,包括你提到的那些(比如壓縮它),但這是我選擇的路線。

編輯:我必須得到一個文件夾,它的子文件夾MD5,這就是我做到這一點。

注:我使用谷歌的番石榴lib中的散列

注:我的代碼編寫的方式,如果文件在目錄中的順序改變

public static String generateMD5(String dir) 
{ 
    File[] files = new File(dir).listFiles(); 
    return Hashing.md5().hashString(expandFiles(files, ""), Charsets.UTF_8).toString(); 
} 

/* Recursive folder expansion */ 
public static String expandFiles(File[] dirFiles, String md5in) 
{ 
    String md5out = md5in; 
    for(File file : dirFiles) 
    { 

     if(file.isHidden()) //For my uses, I wanted to skip any hidden files (.DS_Store was being a problem on a mac) 
     { 
      System.out.println("We have skipped this hidden file: " + file.getName()); 
     } 
     else if (file.isDirectory()) 
     { 
      System.out.println("We are entering a directory recursively: " + file.getName()); 
      md5out += Hashing.md5().hashString(expandFiles(file.listFiles(), md5out), Charsets.UTF_8).toString(); //Recursive call, we have found a subdirectory. 
      System.out.println("We have gotten the md5 of " + file.getName() + " It is " + md5out); 
     } 
     else //We found a file 
     { 
      HashCode md5 = null; 
      try 
      { 
       md5 = Files.hash(file, Hashing.md5()); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      md5out += md5.toString(); 
      System.out.println("We have just gotten the md5 of a specific file: " + file.getName() + ". This file has the md5 of " + md5out); 

     } 
    } 
    return md5out; 
+0

我有一些問題:1。它與文件夾下的文件列表中的順序varing? 2.您是否獲得了文件MD5的MD5校驗和而不是文件MD5? – twlkyao

+0

1.是的,如果文件改變順序,這也會改變目錄的校驗和。 2.是的,我得到所有文件的MD5拼接在一起的md5。如果目錄MD5中的任何文件發生更改,則其md5的字符串的md5也會更改。 – Alexalex1432

+0

我會妥協以預先排序文件並壓縮文件夾。謝謝你們一樣。 – twlkyao

0

您需要: 1.計算目錄 中所有文件的md5 2.計算1步結果的md5。

這裏是你

public static String dirMD5(String dir) 
{ 
    String md5 = ""; 
    File folder = new File(dir); 
    File[] files = folder.listFiles(); 

    for (int i=0; i<files.length; i++) 
    { 
     md5 = md5 + getMd5OfFile(files[i].toString()); 
    } 
    md5 = GetMD5HashOfString(md5); 
    return md5; 
} 


public static String getMd5OfFile(String filePath) 
{ 
    String returnVal = ""; 
    try 
    { 
     InputStream input = new FileInputStream(filePath); 
     byte[]  buffer = new byte[1024]; 
     MessageDigest md5Hash = MessageDigest.getInstance("MD5"); 
     int   numRead = 0; 
     while (numRead != -1) 
     { 
      numRead = input.read(buffer); 
      if (numRead > 0) 
      { 
       md5Hash.update(buffer, 0, numRead); 
      } 
     } 
     input.close(); 

     byte [] md5Bytes = md5Hash.digest(); 
     for (int i=0; i < md5Bytes.length; i++) 
     { 
      returnVal += Integer.toString((md5Bytes[i] & 0xff) + 0x100, 16).substring(1); 
     } 
    } 
    catch(Throwable t) {t.printStackTrace();} 
    return returnVal.toUpperCase(); 
} 

public static String GetMD5HashOfString (String str) 
    { 
     MessageDigest md5 ;   
     StringBuffer hexString = new StringBuffer(); 
     try 
     {        
      md5 = MessageDigest.getInstance("md5");   
      md5.reset(); 
      md5.update(str.getBytes());      
      byte messageDigest[] = md5.digest(); 
      for (int i = 0; i < messageDigest.length; i++) 
      { 
       hexString.append(Integer.toHexString((0xF0 & messageDigest[i])>>4)); 
       hexString.append(Integer.toHexString (0x0F & messageDigest[i])); 
      } 
     } 
     catch (Throwable t) {History.Error(t);}  
     return hexString.toString(); 
    } 
+0

這是否需要目錄中文件的順序? – twlkyao

+0

是的,文件的順序必須相同 – XXX