我有同样的要求,并选择我的“目录哈希”作为目录中所有(非目录)文件的串联流的 MD5 哈希。正如克罗津在评论中提到的类似的问题 https://stackoverflow.com/questions/9169137, 您可以使用SequenceInputStream
充当连接大量其他流的流。我在用着Apache 通用编解码器 http://commons.apache.org/proper/commons-codec对于MD5算法。
基本上,您可以递归目录树,添加FileInputStream
实例到Vector
对于非目录文件。Vector
然后方便地有elements()
方法提供Enumeration
that SequenceInputStream
需要循环遍历。对于 MD5 算法来说,这只是显示为一个InputStream
.
一个问题是,您需要每次都以相同的顺序呈现文件,以便哈希值与相同的输入相同。这listFiles()
中的方法File
不保证顺序,所以我按文件名排序。
我正在为 SVN 控制的文件执行此操作,并且希望避免散列隐藏的 SVN 文件,因此我实现了一个标志来避免隐藏文件。
相关基本代码如下。 (显然它可以被“强化”。)
import org.apache.commons.codec.digest.DigestUtils;
import java.io.*;
import java.util.*;
public String calcMD5HashForDir(File dirToHash, boolean includeHiddenFiles) {
assert (dirToHash.isDirectory());
Vector<FileInputStream> fileStreams = new Vector<FileInputStream>();
System.out.println("Found files for hashing:");
collectInputStreams(dirToHash, fileStreams, includeHiddenFiles);
SequenceInputStream seqStream =
new SequenceInputStream(fileStreams.elements());
try {
String md5Hash = DigestUtils.md5Hex(seqStream);
seqStream.close();
return md5Hash;
}
catch (IOException e) {
throw new RuntimeException("Error reading files to hash in "
+ dirToHash.getAbsolutePath(), e);
}
}
private void collectInputStreams(File dir,
List<FileInputStream> foundStreams,
boolean includeHiddenFiles) {
File[] fileList = dir.listFiles();
Arrays.sort(fileList, // Need in reproducible order
new Comparator<File>() {
public int compare(File f1, File f2) {
return f1.getName().compareTo(f2.getName());
}
});
for (File f : fileList) {
if (!includeHiddenFiles && f.getName().startsWith(".")) {
// Skip it
}
else if (f.isDirectory()) {
collectInputStreams(f, foundStreams, includeHiddenFiles);
}
else {
try {
System.out.println("\t" + f.getAbsolutePath());
foundStreams.add(new FileInputStream(f));
}
catch (FileNotFoundException e) {
throw new AssertionError(e.getMessage()
+ ": file should never not be found!");
}
}
}
}