【发布时间】:2016-07-28 04:32:04
【问题描述】:
如何检查文件内容是否与服务器 perforce JAVA API 中的修订版本相同。在将任何文件更新到 perforce depot 之前,我想检查本地文件和 depot 文件的内容是否有任何差异。如果没有区别,则忽略提交该文件。
【问题讨论】:
如何检查文件内容是否与服务器 perforce JAVA API 中的修订版本相同。在将任何文件更新到 perforce depot 之前,我想检查本地文件和 depot 文件的内容是否有任何差异。如果没有区别,则忽略提交该文件。
【问题讨论】:
我认为你想要 getDiffFiles() 方法:
或者,对于您正在做的特定事情(不提交未更改的文件),只需使用“leaveUnchanged”提交选项,而不是自己做同样的工作。
【讨论】:
SubmitOptions,或者您也可以为您的整个工作区设置SubmitOptions,然后每个提交都以这种方式运行。
是的,做起来很简单。只需生成原始文件的 MD5 哈希,然后在再次更新之前生成新文件的 MD5 哈希。
现在比较两个文件的哈希值。如果两者相同,则两个文件的内容相同,否则它们不同,您可以更新。
这是一个可以轻松生成和检查 MD5 的实用程序,
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);
}
}
}
}
【讨论】:
fstat 命令。