diff --git a/pom.xml b/pom.xml index 41040dd..7dcd370 100644 --- a/pom.xml +++ b/pom.xml @@ -24,16 +24,24 @@ + org.apache.pdfbox pdfbox 3.0.4 + net.sourceforge.tess4j tess4j 5.15.0 + + + commons-cli + commons-cli + 1.9.0 + @@ -50,7 +58,7 @@ true - ru.mcs.udk.UdkScannerExecutor + ru.mcs.udk.UDKSearcher diff --git a/src/main/java/ru/mcs/udk/UDKSearcher.java b/src/main/java/ru/mcs/udk/UDKSearcher.java new file mode 100644 index 0000000..3da80fc --- /dev/null +++ b/src/main/java/ru/mcs/udk/UDKSearcher.java @@ -0,0 +1,89 @@ +package ru.mcs.udk; + +import org.apache.commons.cli.*; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class UDKSearcher { + + public static void main(String[] args) { + // Создание опций командной строки + Options options = new Options(); + options.addOption("path", true, "Путь к папке для поиска файлов"); + options.addOption("format", true, "Формат файлов для поиска (например, pdf, djvu)"); + options.addOption("output", true, "Имя файла для записи результатов"); + + // Парсинг аргументов командной строки + CommandLineParser parser = new DefaultParser(); + try { + CommandLine cmd = parser.parse(options, args); + + // Получение значений параметров + String directoryPath = cmd.getOptionValue("path"); + String fileFormat = cmd.getOptionValue("format"); + String outputFile = cmd.getOptionValue("output"); + + // Проверка наличия обязательных параметров + if (directoryPath == null || fileFormat == null || outputFile == null) { + System.out.println("Необходимо указать все параметры: path, format и output."); + printHelp(options); + return; + } + + // Поиск файлов + List foundFiles = findFilesByExtension(new File(directoryPath), fileFormat.toLowerCase()); + + // Запись результатов в файл + if (foundFiles.isEmpty()) { + System.out.println("Файлы с расширением ." + fileFormat + " не найдены."); + } else { + writeResultsToFile(foundFiles, outputFile); + System.out.println("Результаты записаны в файл: " + outputFile); + } + + } catch (ParseException e) { + System.out.println("Ошибка при разборе аргументов: " + e.getMessage()); + printHelp(options); + } catch (IOException e) { + System.out.println("Ошибка при записи в файл: " + e.getMessage()); + } + } + + private static List findFilesByExtension(File directory, String extension) { + List foundFiles = new ArrayList<>(); + if (directory.isDirectory()) { + File[] files = directory.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isDirectory()) { + // Рекурсивный поиск в подпапках + foundFiles.addAll(findFilesByExtension(file, extension)); + } else if (file.getName().toLowerCase().endsWith("." + extension)) { + // Проверка расширения файла + foundFiles.add(file); + } + } + } + } else { + System.out.println("Указанный путь не является папкой."); + } + return foundFiles; + } + + private static void writeResultsToFile(List files, String outputFile) throws IOException { + try (FileWriter writer = new FileWriter(outputFile)) { + for (File file : files) { + writer.write(file.getAbsolutePath() + "\n"); + } + } + } + + private static void printHelp(Options options) { + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp("UDKSearcher", options); + } +}