Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- import java.util.Stack;
- import java.util.zip.CRC32;
- import java.util.zip.CheckedInputStream;
- public class Main {
- private static long calculateCRC32(File file) {
- long checksum = -1;
- try {
- CheckedInputStream cis = null;
- try {
- cis = new CheckedInputStream(new FileInputStream(file), new CRC32());
- } catch (FileNotFoundException e) {
- System.err.println("Файл не найден");
- System.exit(1);
- }
- byte[] buf = new byte[128];
- while(cis.read(buf) >= 0);
- checksum = cis.getChecksum().getValue();
- } catch (IOException e) {
- System.out.println("Ошибка чтения файла");
- e.printStackTrace();
- System.exit(1);
- }
- return checksum;
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- System.out.println("Введите директорию поиска:");
- String folder = in.nextLine();
- System.out.println("Введите путь к файлу, который нужно найти:");
- String file = in.nextLine();
- File dir = new File(folder);
- File source = new File(file);
- if (!source.exists() || !source.canRead() || source.isDirectory()) {
- System.out.println("Невозможно открыть файл – файл не доступен для чтения.");
- } else if (!dir.exists() || !dir.canRead() || dir.isFile()) {
- System.out.println("Невозможно открыть папку – папка не доступна для чтения.");
- } else {
- Stack<File> stack = new Stack<>();
- stack.push(dir);
- long key = calculateCRC32(source);
- while (!stack.empty()) {
- File cur = stack.pop();
- if (cur.canRead()) {
- for (File f : cur.listFiles()) {
- if (f.canRead()) {
- if (f.isFile()) {
- if (key == calculateCRC32(f)) {
- System.out.println("Найдено совпадение: " + f.getAbsolutePath());
- }
- } else {
- stack.push(f);
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement