2016-07-28 40 views
0

的兩個版本的差異如何檢查文件內容是相同的服務器Perforce的JAVA API的版本。在將任何文件更新到perforce軟件倉庫之前,我想檢查本地文件和軟件倉庫文件的內容是否存在差異。如果沒有區別,則忽略提交該文件。如何採取的Perforce庫文件

+1

[確定是否兩個文件存儲相同的內容]可能的重複(http://stackoverflow.com/questions/27379059/determine-if-two-files-store-the-same-content) –

回答

2

()方法:

https://www.perforce.com/perforce/r15.1/manuals/p4java-javadoc/com/perforce/p4java/impl/mapbased/client/Client.html#getDiffFiles

或者,你是具體的事情做(不提交不變的文件),只需使用「leaveUnchanged」提交選項,而不是自己做同樣的工作。

+0

我喜歡這一個。我會試試看將該選項設置爲「leaveUnchanged」 –

+0

請注意,您可以設置'SubmitOptions'每個單獨提交命令,或者你也可以爲您的整個工作區的'SubmitOptions',然後每提交行爲這種方式。 –

2

是簡單的事情。只需生成原始文件的MD5散列,並在更新之前再次生成新文件的MD5散列。

現在比較的哈希值這兩個文件。如果兩者都是相同的,那麼這兩個文件的內容是相同的,如果不是,那麼它們是不同的,並且你很好地更新。

這裏是生成和輕鬆地檢查MD5我想你想的getDiffFiles的效用,

public class MD5Utils { 
    private static final String TAG = "MD5"; 

    public static boolean checkMD5(String md5, File updateFile) { 
     if (TextUtils.isEmpty(md5) || updateFile == null) { 
      Log.e(TAG, "MD5 string empty or updateFile null"); 
      return false; 
     } 

     String calculatedDigest = calculateMD5(updateFile); 
     if (calculatedDigest == null) { 
      Log.e(TAG, "calculatedDigest null"); 
      return false; 
     } 

     Log.v(TAG, "Calculated digest: " + calculatedDigest); 
     Log.v(TAG, "Provided digest: " + md5); 

     return calculatedDigest.equalsIgnoreCase(md5); 
    } 

    public static String calculateMD5(File updateFile) { 
     MessageDigest digest; 
     try { 
      digest = MessageDigest.getInstance("MD5"); 
     } catch (NoSuchAlgorithmException e) { 
      Log.e(TAG, "Exception while getting digest", e); 
      return null; 
     } 

     InputStream is; 
     try { 
      is = new FileInputStream(updateFile); 
     } catch (FileNotFoundException e) { 
      Log.e(TAG, "Exception while getting FileInputStream", e); 
      return null; 
     } 

     byte[] buffer = new byte[8192]; 
     int read; 
     try { 
      while ((read = is.read(buffer)) > 0) { 
       digest.update(buffer, 0, read); 
      } 
      byte[] md5sum = digest.digest(); 
      BigInteger bigInt = new BigInteger(1, md5sum); 
      String output = bigInt.toString(16); 
      // Fill to 32 chars 
      output = String.format("%32s", output).replace(' ', '0'); 
      return output; 
     } catch (IOException e) { 
      throw new RuntimeException("Unable to process file for MD5", e); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       Log.e(TAG, "Exception on closing MD5 input stream", e); 
      } 
     } 
    } 
} 
+0

感謝您的答覆。但是我們不能用Perforce JAVA API來做同樣的事情嗎? –

+1

恐怕你不能。你必須使用標準的JAVA API。 –

+0

感謝您的及時響應 –