private List<Path> getPathsFromFile(String file, List<Path> filesToCombine) throws IOException { Path filePath = Paths.get(file); File fileLister = filePath.toFile(); BufferedReader br = new BufferedReader(new FileReader(fileLister)); String line; PathMatcher matcher; Finder finder = new Finder(); String parentFolder; Path filePathToCombine; while ((line = br.readLine()) != null) { if (line.isEmpty()) { continue; } parentFolder = ""; filePathToCombine = Paths.get(line); if (filePathToCombine.getParent() != null) { parentFolder = filePathToCombine.getParent().toString(); } matcher = FileSystems.getDefault() .getPathMatcher("glob:" + filePathToCombine.getFileName().toString()); finder.setMatcher(matcher); Files.walkFileTree( Paths.get(fileLister.getAbsoluteFile().getParent() + parentFolder), finder); } filesToCombine.addAll(finder.getMatchPath()); return filesToCombine; }
private void compareBinaryFolder(String path, boolean res) throws BrutException, IOException { String tmp = ""; if (res) { tmp = File.separatorChar + "res" + File.separatorChar; } Files.walkFileTree( Paths.get(sTestOrigDir.toPath() + tmp + path), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { // hacky fix - load test by changing name of control File control = file.toFile(); File test = new File(file.toString().replace("testapp-orig", "testapp-new")); if (test.isFile()) { if (control.hashCode() != test.hashCode()) { sResult = false; return FileVisitResult.TERMINATE; } } else { sResult = false; return FileVisitResult.TERMINATE; } return FileVisitResult.CONTINUE; } }); }
/** Register the given directory, and all its sub-directories, with the WatchService.f */ private void registerAll(final Path start) throws IOException { Files.walkFileTree( start, new SimpleFileVisitor<Path>() { @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { logger.debug("postVisitDir " + dir.toString()); WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); logger.debug("Adding key: " + key.toString() + " " + dir); keys.put(key, dir); return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Path fullDir, BasicFileAttributes attrs) throws IOException { logger.debug("preVisitDirectory " + fullDir.toString()); String dir = "/" + fixPath(startDir.relativize(fullDir).toString()); if (!MetaHandler.isStored(dir) || MetaHandler.isModified(dir, fullDir.toFile())) { try { Entry folderEntry = null; try { logger.debug("Creating folder: " + dir); folderEntry = api.createFolder(dir); } catch (DropboxServerException ex) { if (ex.error == DropboxServerException._403_FORBIDDEN) { folderEntry = api.metadata(dir, 0, null, false, null); } else { logger.error(ex.getMessage()); } } MetaHandler.setFile(dir, fullDir.toFile(), folderEntry.rev); } catch (DropboxException ex) { ex.printStackTrace(); } } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path fullPath, BasicFileAttributes attrs) throws IOException { logger.debug("visitFile " + fullPath.toString()); String file = "/" + fixPath(startDir.relativize(fullPath).toString()); if (!MetaHandler.isStored(file) || MetaHandler.isModified(file, fullPath.toFile())) { logger.debug("Not stored or updated: " + file); try { uploadFile(file, fullPath); } catch (Exception ex) { ex.printStackTrace(); } } return FileVisitResult.CONTINUE; } }); }
public int countFiles(String root) { CountFile fileProcessor = new CountFile(); try { Files.walkFileTree(Paths.get(root), fileProcessor); } catch (IOException e) { e.printStackTrace(); } return fileProcessor.getTotal(); }
/** Регистрация директории для WatchService. */ private void registerAll(final Path start) throws IOException { // Регистрируем директорию Files.walkFileTree( start, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { register(dir); return FileVisitResult.CONTINUE; } }); }
public static void load(Collection<FileDesc> files, Path root, int blocSize, Pattern pattern) throws IOException { root = root.toAbsolutePath().normalize(); Visitor visitor = new Visitor(root, blocSize, pattern); Files.walkFileTree(root, visitor); for (Future<FileDesc> future : visitor.futures()) { try { files.add(future.get()); } catch (Exception e) { log.error("", e); } } }
public void backup(String wildCardPath) { try { if (wildCardPath.contains("*") || wildCardPath.contains("?")) { // Extract the path so we know where to start and get the name which contains the // wildcard so we know what to store: File wildCardFile = new File(wildCardPath); String parentDirectory = wildCardFile.getParent(); String wildCardPattern = wildCardFile.getName(); _pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + wildCardPattern); Files.walkFileTree(Paths.get(parentDirectory), this); } else storeFile(new File(wildCardPath)); } catch (FileNotFoundException exception) { Console.printError("File/Directory not found: " + wildCardPath); } catch (IOException exception) { Console.printError( "File/Directory '%s' not stored because: '%s'", wildCardPath, exception.getMessage()); } }
public void clearDirectories() { try { SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); final String sessionDate = df.format(new Date()); Path startPath = Paths.get(ABS_OUTPUT_DIR); logger.info("Начинаем очистку каталога " + ABS_OUTPUT_DIR); Files.walkFileTree( startPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { logger.info("Обнаружили файл" + file); if (file.toString().endsWith(".zip")) { Path fileTo = Paths.get(ABS_ARCH_OUT_DIR + "/" + sessionDate + "-" + file.getFileName()); Files.move(file, fileTo); logger.info("Скопирован файл " + file + " -> " + fileTo); } else { logger.info("Удаляем файл " + file); Files.delete(file); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { logger.info("Обнаружили каталог" + dir); if (!dir.toString().equals(ABS_OUTPUT_DIR)) { logger.info("Удаляем каталог " + dir); Files.delete(dir); } return FileVisitResult.CONTINUE; } }); logger.info("Закончили очистку каталога " + ABS_OUTPUT_DIR); } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); } }
/** * Compresses the specified directory to a zip file * * @param dir the directory to compress * @return the compressed file * @throws IOException */ public static Path compress(Path dir) throws IOException { Assert.isTrue(Files.exists(dir), "The directory does not exist: " + dir.toAbsolutePath()); Assert.isTrue(Files.isDirectory(dir), "Should be a directory: " + dir.toAbsolutePath()); Path result = Paths.get(dir.toAbsolutePath() + FileType.DOT_ZIP); try (final ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(result.toFile())))) { // out.setMethod(ZipOutputStream.DEFLATED); final byte data[] = new byte[BUFFER]; // get a list of files from current directory Files.walkFileTree( dir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs) throws IOException { final File file = path.toFile(); // compress to relative directory, not absolute final String root = StringUtils.substringAfter(file.getParent(), dir.toString()); try (final BufferedInputStream origin = new BufferedInputStream(new FileInputStream(file), BUFFER)) { final ZipEntry entry = new ZipEntry(root + File.separator + path.getFileName()); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } } return FileVisitResult.CONTINUE; } }); } return result; }