码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA-读取文件部分内容计算HASH值

时间:2015-01-21 19:53:33      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

 

对于一些大文件,有时会需要计算部分内容的Hash,下面的函数计算了 文件头尾各1M,中间跳跃100M取10K 以及文件大小的Hash值

public static String CalHash(String path) throws IOException 
    {
        File file = new File(path);
        if (!file.canRead())
            return "";
        if (file.length() < 150 * 1024 * 1024) {
            return "";
        }
        
        //FileInputStream in = new FileInputStream(file);
        RandomAccessFile in = new RandomAccessFile(file, "r");
        
        MessageDigest messagedigest;
        try {
            messagedigest = MessageDigest.getInstance("SHA1");
            int szRead = 0;
            int size_1M = 1024*1024; 
            byte[] buffer = new byte[size_1M];
            // 文件头尾各1M,中间跳跃100M取10K算Hash
            // 开头
            szRead = in.read(buffer);
            messagedigest.update(buffer);
            // 结尾
            in.seek(file.length() - size_1M);
            szRead = in.read(buffer);
            messagedigest.update(buffer);

            int BUFFER_SIZE = (10 * 1024);
            buffer = new byte[(int) BUFFER_SIZE];

            long endPos = file.length() - 1024 * 1024 - BUFFER_SIZE;
            long skipSize = (long) ( ((float) (file.length() / (float) (100.0 * 1024.0 * 1024.0) - (float) 1.0) * BUFFER_SIZE) );
            long currentPos = 1024 * 1024;
            in.seek(currentPos);
            while (in.getFilePointer() < endPos && in.read(buffer) > 0) {
                in.seek(in.getFilePointer() + skipSize);                
                messagedigest.update(buffer);
            }

            // 计算文件size的hash
            long fsize = file.length();
            byte[] fSizeBytes = getBytes(fsize);
            messagedigest.update(fSizeBytes);
            return byte2hex(messagedigest.digest());
            
        } catch (NoSuchAlgorithmException e) {
        } catch (OutOfMemoryError e) {
        } finally {
            in.close();
        }
        return null;
    }

 

JAVA-读取文件部分内容计算HASH值

标签:

原文地址:http://www.cnblogs.com/inbase/p/4239611.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!