如何获得的Android / Java中的文件目录的MD5校验 [英] How to get a file directory's MD5 checksum in Android/Java
本文介绍了如何获得的Android / Java中的文件目录的MD5校验的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望得到一个文件目录的MD5校验,我已经得到一个文件的算法,但是当我用它到一个目录它导致空。我怎样才能快速得到检验。
以下是我的文件(从匿名stackoverflower编者)algotithm。
I want to get an MD5 checksum of a file directory, I have already get the algorithm for a file, but it results null when I use it to a directory. How can I get the checksum quickly. The following are my algotithm for a file(Edited from the anonymous stackoverflower).
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:
I searched that there are some solutings:
- pre-顺序目录下的文件。
- 的COM preSS目录到为.zip或.rar程序存档文件等,并校验它。
- 获取所有的目录到一个流中的内容,并校验它。
我不知道是否有一些方便的solutions.Thank你提前。
I wonder whether there are some convenient solutions.Thank you in advance.
推荐答案
据我所知,没有一个有效的方法来得到一个目录的校验和。
As far as I know there isn't an efficient way to get the checksum of a directory.
这篇关于如何获得的Android / Java中的文件目录的MD5校验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文