首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Android/Java中获取文件目录的MD5校验和

如何在Android/Java中获取文件目录的MD5校验和
EN

Stack Overflow用户
提问于 2013-11-29 07:03:17
回答 3查看 8.1K关注 0票数 1

我想得到一个文件目录的MD5校验和,我已经得到了一个文件的算法,但是当我将它用于一个目录时,它的结果为null。我怎样才能快速得到支票和。下面是我对一个文件(从匿名堆栈花中编辑)的协商。

代码语言:javascript
运行
复制
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) { }
            }
        }
    }

我找到了一些解决办法:

  1. 预先订购目录下的文件。
  2. 将目录压缩为存档文件(如.zip或.rar ),并对其进行校验。
  3. 将目录下的所有内容放入流中,并对其进行校验。

我想知道是否提前有方便的solutions.Thank您。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-01-04 09:09:37

据我所知,没有一种有效的方法来获取目录的校验和。

票数 0
EN

Stack Overflow用户

发布于 2014-05-23 20:56:37

您需要: 1.计算目录2中所有文件的md5。从1步计算结果的md5。

这是给你的

代码语言:javascript
运行
复制
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();
    }
票数 2
EN

Stack Overflow用户

发布于 2013-12-18 00:00:40

我只是这样做的,但我这样做的一个目录,我知道将是平坦的(没有子目录)

  • 遍历目录文件
  • 获取每个文件的MD5
  • 将该MD5追加到字符串中
  • 在对文件进行迭代之后,我得到了字符串的MD5 (包含所有其他文件的MD5s )

这会影响到,如果任何文件要更改,这个“目录”md5也会更改。

我知道还有其他的方法,包括你提到的方法(比如拉链),但这是我选择的路线。

编辑:我必须得到一个文件夹,它是子文件夹,md5,这就是我如何实现的。

注意:我使用了谷歌的番石榴库作为散列

注意:如果目录中的文件顺序改变了,那么我的代码的编写方式就会改变。

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20280112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档