Example usage for java.nio.file Files walkFileTree

List of usage examples for java.nio.file Files walkFileTree

Introduction

In this page you can find the example usage for java.nio.file Files walkFileTree.

Prototype

public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException 

Source Link

Document

Walks a file tree.

Usage

From source file:org.cryptomator.frontend.webdav.servlet.DavFolder.java

private void copyInternal(DavNode destination, boolean shallow) throws DavException {
    assert exists();
    assert attr.isPresent();
    if (!Files.isDirectory(destination.path.getParent())) {
        throw new DavException(DavServletResponse.SC_CONFLICT, "Destination's parent doesn't exist.");
    }/* w  w w .ja va  2  s  . c  o m*/

    try {
        if (shallow && destination instanceof DavFolder) {
            // http://www.webdav.org/specs/rfc2518.html#copy.for.collections
            Files.createDirectory(destination.path);
            BasicFileAttributeView attrView = Files.getFileAttributeView(destination.path,
                    BasicFileAttributeView.class);
            if (attrView != null) {
                BasicFileAttributes a = attr.get();
                attrView.setTimes(a.lastModifiedTime(), a.lastAccessTime(), a.creationTime());
            }
        } else {
            Files.walkFileTree(path,
                    new CopyingFileVisitor(path, destination.path, StandardCopyOption.REPLACE_EXISTING));
        }
    } catch (IOException e) {
        throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, e);
    }
}

From source file:de.ncoder.studipsync.Syncer.java

public boolean isSeminarInSync(Seminar seminar) throws IOException, StudipException {
    if (!checkLevel.includes(CheckLevel.Count)) {
        return true;
    }//from   w ww .j av a 2s. co  m

    //List downloads
    final List<Download> downloads;
    browserLock.lock();
    try {
        downloads = adapter.parseDownloads(PAGE_DOWNLOADS_LATEST, false);
    } finally {
        browserLock.unlock();
    }
    if (downloads.isEmpty()) {
        //No downloads - nothing to do
        return true;
    }

    //List local files
    final List<Path> localFiles = new LinkedList<>();
    final Path storagePath = storage.resolve(seminar);
    if (!Files.exists(storagePath)) {
        //No local files despite available downloads
        log.info(marker, "Seminar is empty!");
        return false;
    }
    Files.walkFileTree(storagePath, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            localFiles.add(file);
            return FileVisitResult.CONTINUE;
        }
    });

    //Count local files
    if (localFiles.size() < downloads.size()) {
        // Missing files
        log.warn(marker, "Seminar has only " + localFiles.size() + " local file(s) of " + downloads.size()
                + " online file(s).");
        return false;
    } else {
        // Ignore surplus files
        log.debug(marker, "Seminar has deleted file(s) left! " + localFiles.size() + " local file(s) and "
                + downloads.size() + " online file(s).");
    }

    //Check local files
    if (!areFilesInSync(downloads, localFiles)) {
        return false;
    }

    return true;
}

From source file:org.commonjava.test.compile.CompilerFixture.java

public List<File> scan(final File directory, final String pattern) throws IOException {
    final PathMatcher matcher = FileSystems.getDefault()
            .getPathMatcher("glob:" + directory.getCanonicalPath() + "/" + pattern);

    final List<File> sources = new ArrayList<>();
    Files.walkFileTree(directory.toPath(), new SimpleFileVisitor<Path>() {

        @Override/*w ww  .  ja  v  a2  s.c  o  m*/
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            if (matcher.matches(file)) {
                sources.add(file.toFile());
            }

            return FileVisitResult.CONTINUE;
        }

    });

    return sources;
}

From source file:org.elasticsearch.plugins.PluginManagerIT.java

/** creates a plugin .zip and bad checksum file and returns the url for testing */
private String createPluginWithBadChecksum(final Path structure, String... properties) throws IOException {
    writeProperties(structure, properties);
    Path zip = createTempDir().resolve(structure.getFileName() + ".zip");
    try (ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(zip))) {
        Files.walkFileTree(structure, new SimpleFileVisitor<Path>() {
            @Override// w w w .  jav a2s. c o m
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                stream.putNextEntry(new ZipEntry(structure.relativize(file).toString()));
                Files.copy(file, stream);
                return FileVisitResult.CONTINUE;
            }
        });
    }
    if (randomBoolean()) {
        writeSha1(zip, true);
    } else {
        writeMd5(zip, true);
    }
    return zip.toUri().toURL().toString();
}

From source file:de.alexkamp.sandbox.ChrootSandbox.java

@Override
public void walkDirectoryTree(String basePath, final DirectoryWalker walker) throws IOException {
    final int baseNameCount = data.getBaseDir().toPath().getNameCount();

    File base = new File(data.getBaseDir(), basePath);

    Files.walkFileTree(base.toPath(), new FileVisitor<Path>() {
        @Override/* w w w. j av  a 2s. c om*/
        public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes basicFileAttributes)
                throws IOException {
            if (walker.visitDirectory(calcSubpath(path))) {
                return FileVisitResult.CONTINUE;
            }
            return FileVisitResult.SKIP_SUBTREE;
        }

        private String calcSubpath(Path path) {
            if (path.getNameCount() == baseNameCount) {
                return "/";
            }
            return "/" + path.subpath(baseNameCount, path.getNameCount()).toString();
        }

        @Override
        public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes)
                throws IOException {
            String subpath = calcSubpath(path);
            if (walker.visitFile(subpath)) {
                try (InputStream is = Files.newInputStream(path)) {
                    walker.visitFileContent(subpath, is);
                }
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path path, IOException e) throws IOException {
            if (walker.failed(e)) {
                return FileVisitResult.CONTINUE;
            } else {
                return FileVisitResult.TERMINATE;
            }
        }

        @Override
        public FileVisitResult postVisitDirectory(Path path, IOException e) throws IOException {
            return FileVisitResult.CONTINUE;
        }
    });
}

From source file:org.fao.geonet.api.mapservers.GeoFile.java

/**
 * Returns the names of the raster layers (GeoTIFFs) in the geographic file.
 *
 * @return a collection of layer names// w w w .  ja  va  2s.co  m
 */
public Collection<String> getRasterLayers() throws IOException {
    final LinkedList<String> layers = new LinkedList<String>();
    if (zipFile != null) {
        for (Path path : zipFile.getRootDirectories()) {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    String fileName = file.getFileName().toString();
                    if (fileIsGeotif(fileName)) {
                        layers.add(getBase(fileName));
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    } else {
        String fileName = file.getFileName().toString();
        if (fileIsGeotif(fileName)) {
            layers.add(getBase(fileName));
        }
    }
    return layers;
}

From source file:com.kegare.caveworld.util.CaveUtils.java

public static boolean archiveDirZip(final File dir, final File dest) {
    final Path dirPath = dir.toPath();
    final String parent = dir.getName();
    Map<String, String> env = Maps.newHashMap();
    env.put("create", "true");
    URI uri = dest.toURI();//from   ww w.ja  v a 2s . com

    try {
        uri = new URI("jar:" + uri.getScheme(), uri.getPath(), null);
    } catch (Exception e) {
        return false;
    }

    try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
        Files.createDirectory(zipfs.getPath(parent));

        for (File file : dir.listFiles()) {
            if (file.isDirectory()) {
                Files.walkFileTree(file.toPath(), new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        Files.copy(file, zipfs.getPath(parent, dirPath.relativize(file).toString()),
                                StandardCopyOption.REPLACE_EXISTING);

                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                            throws IOException {
                        Files.createDirectory(zipfs.getPath(parent, dirPath.relativize(dir).toString()));

                        return FileVisitResult.CONTINUE;
                    }
                });
            } else {
                Files.copy(file.toPath(), zipfs.getPath(parent, file.getName()),
                        StandardCopyOption.REPLACE_EXISTING);
            }
        }

        return true;
    } catch (Exception e) {
        e.printStackTrace();

        return false;
    }
}

From source file:org.apache.tika.batch.fs.FSBatchTestBase.java

public static void deleteDirectory(Path dir) throws IOException {
    Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
        @Override//from  w  w w . j a v  a 2 s .co m
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }

    });
}

From source file:de.prozesskraft.pkraft.Waitinstance.java

/**
 * ermittelt alle process binaries innerhalb eines directory baumes
 * @param pathScandir/*  ww w.j  a  va  2 s .  c o  m*/
 * @return
 */
private static String[] getProcessBinaries(String pathScandir) {
    final ArrayList<String> allProcessBinaries = new ArrayList<String>();

    // den directory-baum durchgehen und fuer jeden eintrag ein entity erstellen
    try {
        Files.walkFileTree(Paths.get(pathScandir), new FileVisitor<Path>() {
            // called after a directory visit is complete
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            // called before a directory visit
            public FileVisitResult preVisitDirectory(Path walkingDir, BasicFileAttributes attrs)
                    throws IOException {
                return FileVisitResult.CONTINUE;
            }

            // called for each file visited. the basic file attributes of the file are also available
            public FileVisitResult visitFile(Path walkingFile, BasicFileAttributes attrs) throws IOException {
                // ist es ein process.pmb file?
                if (walkingFile.endsWith("process.pmb")) {
                    allProcessBinaries.add(new java.io.File(walkingFile.toString()).getAbsolutePath());
                }

                return FileVisitResult.CONTINUE;
            }

            // called for each file if the visit failed
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

        });
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return allProcessBinaries.toArray(new String[allProcessBinaries.size()]);
}

From source file:com.google.cloud.dataflow.sdk.io.TextIOTest.java

License:asdf

@AfterClass
public static void testdownClass() throws IOException {
    Files.walkFileTree(tempFolder, new SimpleFileVisitor<Path>() {
        @Override//from  ww w.  j  av  a 2 s .  com
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }
    });
}