如何获取文件目录';Android/Java中的MD5校验和
我想得到一个文件目录的MD5校验和,我已经得到了一个文件的算法,但是当我把它用于一个目录时,结果是空的。如何快速获取校验和。 以下是我的文件算法(从匿名stackoverflower编辑) 我发现有一些解决方案:如何获取文件目录';Android/Java中的MD5校验和,java,android,md5,checksum,Java,Android,Md5,Checksum,我想得到一个文件目录的MD5校验和,我已经得到了一个文件的算法,但是当我把它用于一个目录时,结果是空的。如何快速获取校验和。 以下是我的文件算法(从匿名stackoverflower编辑) 我发现有一些解决方案: 对目录下的文件进行预排序 将目录压缩到.zip或.rar等存档文件中,并对其进行校验和 将目录下的所有内容放入流中,并对其进行校验和 我想知道是否有一些方便的解决方案。提前谢谢你 我只是这样做,但我这样做是为了一个我知道将是平面的目录(没有子目录) 遍历目录文件 获取每个文件的MD5
我想知道是否有一些方便的解决方案。提前谢谢你 我只是这样做,但我这样做是为了一个我知道将是平面的目录(没有子目录)
- 遍历目录文件
- 获取每个文件的MD5
- 将MD5追加到字符串中
- 在对文件进行迭代之后,我得到了字符串的MD5,该字符串包含所有其他文件的MD5
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;
我只是这样做,但我这样做是为了一个我知道将是平面的目录(没有子目录)
- 遍历目录文件
- 获取每个文件的MD5
- 将MD5追加到字符串中
- 在对文件进行迭代之后,我得到了字符串的MD5,该字符串包含所有其他文件的MD5
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;
我只是这样做,但我这样做是为了一个我知道将是平面的目录(没有子目录)
- 遍历目录文件
- 获取每个文件的MD5
- 将MD5追加到字符串中
- 在对文件进行迭代之后,我得到了字符串的MD5,该字符串包含所有其他文件的MD5
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;
我只是这样做,但我这样做是为了一个我知道将是平面的目录(没有子目录)
- 遍历目录文件
- 获取每个文件的MD5
- 将MD5追加到字符串中
- 在对文件进行迭代之后,我得到了字符串的MD5,该字符串包含所有其他文件的MD5
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;
据我所知,没有一种有效的方法来获取目录的校验和。据我所知,没有一种有效的方法来获取目录的校验和。据我所知,没有一种有效的方法来获取目录的校验和。据我所知,没有一种有效的方法来获取目录的校验和目录。您需要: 1.计算目录中所有文件的md5 2.计算第一步结果的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();
}
publicstaticstringdirmd5(stringdir)
{
字符串md5=“”;
文件夹=新文件(目录);
File[]files=文件夹.listFiles();
对于(int i=0;i 0)
{
更新(缓冲区,0,numRead);
}
}
input.close();
字节[]md5Bytes=md5Hash.digest();
对于(int i=0;i>4));
append(Integer.toHexString(0x0F&messageDigest[i]);
}
}
捕获(可丢弃的){History.Erro