栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java扫描全盘文件hash值

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java扫描全盘文件hash值

public class FullFileScanningImpl {

    public static List findFile() throws IOException {
        List list = new ArrayList();
        File[] f = getSystem();
        for (int i = 0; i < f.length; i++) {
             getFileName(f[i],list);
        }
        return list;
    }

    
    private static File[] getSystem() {
        File[] file = File.listRoots();
        return file;
    }

    
    private static void getFileName(File file ,List list) throws IOException {
        File[] f = file.listFiles();
        //判断f数组是否为空
        if (f != null && f.length > 0) {
            for (File files : f) {
                if (files.isDirectory()) { //判断是文件夹
                    getFileName(files,list);
                } else {
                     StringBuffer sb = new StringBuffer("type=1;subtype=15;");
                    if (files.renameTo(files)) {
                        try {
                            sb.append("path=").append(files.getPath()).append(";").append("hash=").append(md5HashCode32(files.getPath()));
                            // map.put(files.getPath(),md5HashCode32(files.getPath()));
                        } catch (Exception e) {
                            //针对某些特殊文件可改名  但是不可打开
                            // map.put("path="+files.getPath(),"hash="+copyFile(files));
                            sb.append("path=").append(files.getPath()).append(";").append("hash=").append(copyFile(files));
                        }
                    }else{
                        sb.append("path=").append(files.getPath()).append(";").append("hash=").append(copyFile(files));
                    }

                    list.add(sb.toString());
                    sb.setLength(0);
                }

            }
        }
    }

    public static String md5HashCode32(String filePath) throws FileNotFoundException {
        FileInputStream fis = new FileInputStream(filePath);
        return md5HashCode32(fis);
    }

    public static String md5HashCode32(InputStream fis) {
        try {
            //拿到一个MD5转换器,如果想使用SHA-1或SHA-256,则传入SHA-1,SHA-256
            MessageDigest md = MessageDigest.getInstance("MD5");
            //分多次将一个文件读入,对于大型文件而言,比较推荐这种方式,占用内存比较少。
            byte[] buffer = new byte[1024];
            int length = -1;
            while ((length = fis.read(buffer, 0, 1024)) != -1) {
                md.update(buffer, 0, length);
            }
            fis.close();
            byte[] md5Bytes = md.digest();
            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            return hexValue.toString();
        } catch (Exception e) {
            return "";
        }
    }


    public static String copyFile(File files) {
        String fileMd5 = "";
        try {
            File[] fileTmp = getSystem();
            if (fileTmp.length > 0) {
                StringBuffer sb = new StringBuffer();
                sb.append(fileTmp[0]);
                sb.append(System.getProperty("file.separator"));
                sb.append("newpaths");
                File newpath = new File(sb.toString());
                if (!newpath.exists()) {
                    newpath.mkdir();
                }
                sb.append(System.getProperty("file.separator"));
                sb.append(files.getName());
                sb.append("_tmp");
                File newpaths = new File(sb.toString());
                if (!newpaths.exists()) {
                    Files.copy(files.toPath(), newpaths.toPath());
                } else {
                    newpaths.delete();
                    Files.copy(files.toPath(), newpaths.toPath());
                }
                fileMd5 = md5HashCode32(newpaths.getPath());
                newpaths.delete();
            }
        } catch (Exception e) {
            return "";
        }
        return fileMd5;
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/877480.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号