Copilot生成java代码找重复相片

原理

通过把照片进行hash转换,相同hash值的认为是相同照片,打印到本地文件中供二次确认。

代码

import java.io.*;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

public class RemoveDuplicatePhotos {
    public static void main(String[] args) {
        File folder = new File("/path/to/your/photos"); // 替换为你的照片所在的文件夹
        Map<String, String> map = new ConcurrentHashMap<>();
        ExecutorService executor = Executors.newFixedThreadPool(10); // 创建一个固定大小的线程池
        try (PrintWriter writer = new PrintWriter(new File("duplicates.txt"))) {
            processFolder(folder, map, writer, executor);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        executor.shutdown(); // 关闭线程池
    }

    private static void processFolder(File folder, Map<String, String> map, PrintWriter writer, ExecutorService executor) {
        File[] files = folder.listFiles();
        if (files == null) {
            System.out.println("Folder does not exist or is not a directory");
            return;
        }

        for (File file : files) {
            if (file.isDirectory()) {
                processFolder(file, map, writer, executor);
            } else {
                executor.submit(() -> {
                    try {
                        String hash = calculateHash(file, "SHA-256");
                        String existingFile = map.putIfAbsent(hash, file.getPath());
                        if (existingFile != null) {
                            writer.println("Duplicate file found: " + file.getPath());
                            writer.println("It is a duplicate of: " + existingFile);
                        }
                    } catch (IOException | NoSuchAlgorithmException e) {
                        e.printStackTrace();
                    }
                });
            }
        }
    }

    private static String calculateHash(File file, String algorithm) throws IOException, NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        try (DigestInputStream dis = new DigestInputStream(new FileInputStream(file), md)) {
            byte[] buffer = new byte[1024];
            while (dis.read(buffer) != -1) ; //empty loop to clear the data
            md = dis.getMessageDigest();
        }

        // bytes to hex
        BigInteger bi = new BigInteger(1, md.digest());
        return String.format("%032x", bi);
    }
}

判断某个文件夹的相片在其他文件夹中是否全都有

import java.io.*;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

public class CheckFilesExist {
    public static void main(String[] args) {
        File folder1 = new File("/path/to/your/first/folder"); // 替换为你的第一个文件夹
        File folder2 = new File("/path/to/your/second/folder"); // 替换为你的第二个文件夹
        Map<String, String> map1 = new ConcurrentHashMap<>();
        Map<String, String> map2 = new ConcurrentHashMap<>();
        ExecutorService executor = Executors.newFixedThreadPool(10); // 创建一个固定大小的线程池
        processFolder(folder1, map1, folder2, executor);
        processFolder(folder2, map2, null, executor);
        executor.shutdown(); // 关闭线程池
        while (!executor.isTerminated()) ; // 等待所有任务完成

        for (String hash : map2.keySet()) {
            if (!map1.containsKey(hash)) {
                System.out.println("File " + map2.get(hash) + " does not exist in the first folder");
            }
        }
    }

    private static void processFolder(File folder, Map<String, String> map, File excludeFolder, ExecutorService executor) {
        File[] files = folder.listFiles();
        if (files == null) {
            System.out.println("Folder does not exist or is not a directory");
            return;
        }

        for (File file : files) {
            if (file.isDirectory()) {
                if (excludeFolder != null && file.equals(excludeFolder)) {
                    continue;
                }
                processFolder(file, map, excludeFolder, executor);
            } else {
                executor.submit(() -> {
                    try {
                        String hash = calculateHash(file, "SHA-256");
                        map.put(hash, file.getPath());
                    } catch (IOException | NoSuchAlgorithmException e) {
                        e.printStackTrace();
                    }
                });
            }
        }
    }

    private static String calculateHash(File file, String algorithm) throws IOException, NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        try (DigestInputStream dis = new DigestInputStream(new FileInputStream(file), md)) {
            byte[] buffer = new byte[1024];
            while (dis.read(buffer) != -1) ; //empty loop to clear the data
            md = dis.getMessageDigest();
        }

        // bytes to hex
        BigInteger bi = new BigInteger(1, md.digest());
        return String.format("%032x", bi);
    }
}
  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值