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, Set<FileVisitOption> options, int maxDepth,
        FileVisitor<? super Path> visitor) throws IOException 

Source Link

Document

Walks a file tree.

Usage

From source file:org.esa.nest.util.FileIOUtils.java

public static void moveFolder(final Path source, final Path target) throws IOException {
    // follow links when copying files
    final EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
    final CopyDirVisitor tc = new CopyDirVisitor(source, target, true);
    Files.walkFileTree(source, opts, Integer.MAX_VALUE, tc);
}

From source file:com.spotify.docker.client.CompressedDirectory.java

/**
 * This method creates a gzip tarball of the specified directory. File permissions will be
 * retained. The file will be created in a temporary directory using the {@link
 * Files#createTempFile(String, String, FileAttribute[])} method. The returned object is
 * auto-closeable, and upon closing it, the archive file will be deleted.
 *
 * @param directory the directory to compress
 * @return a Path object representing the compressed directory
 * @throws IOException if the compressed directory could not be created.
 *///from  w  w  w  .  j  a v a  2s  .c o m
public static CompressedDirectory create(final Path directory) throws IOException {
    final Path file = Files.createTempFile("docker-client-", ".tar.gz");

    final Path dockerIgnorePath = directory.resolve(".dockerignore");
    final ImmutableSet<PathMatcher> ignoreMatchers = parseDockerIgnore(dockerIgnorePath);

    try (final OutputStream fileOut = Files.newOutputStream(file);
            final GzipCompressorOutputStream gzipOut = new GzipCompressorOutputStream(fileOut);
            final TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gzipOut)) {
        tarOut.setLongFileMode(LONGFILE_POSIX);
        tarOut.setBigNumberMode(BIGNUMBER_POSIX);
        Files.walkFileTree(directory, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                new Visitor(directory, ignoreMatchers, tarOut));

    } catch (Throwable t) {
        // If an error occurs, delete temporary file before rethrowing exception.
        try {
            Files.delete(file);
        } catch (IOException e) {
            // So we don't lose track of the reason the file was deleted... might be important
            t.addSuppressed(e);
        }

        throw t;
    }

    return new CompressedDirectory(file);
}

From source file:org.zanata.sync.jobs.cache.RepoCacheImpl.java

private long copyDir(Path source, Path target) throws IOException {
    Files.createDirectories(target);
    AtomicLong totalSize = new AtomicLong(0);
    Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
            new SimpleFileVisitor<Path>() {
                @Override/*from   w  w w. j  a v  a 2s  .c  o m*/
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
                    Path targetdir = target.resolve(source.relativize(dir));
                    try {
                        if (Files.isDirectory(targetdir) && Files.exists(targetdir)) {
                            return CONTINUE;
                        }
                        Files.copy(dir, targetdir, StandardCopyOption.REPLACE_EXISTING,
                                StandardCopyOption.COPY_ATTRIBUTES);
                    } catch (FileAlreadyExistsException e) {
                        if (!Files.isDirectory(targetdir)) {
                            throw e;
                        }
                    }
                    return CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    if (Files.isRegularFile(file)) {
                        totalSize.accumulateAndGet(Files.size(file), (l, r) -> l + r);
                    }
                    Path targetFile = target.resolve(source.relativize(file));

                    // only copy to target if it doesn't exist or it exist but the content is different
                    if (!Files.exists(targetFile)
                            || !com.google.common.io.Files.equal(file.toFile(), targetFile.toFile())) {
                        Files.copy(file, targetFile, StandardCopyOption.REPLACE_EXISTING,
                                StandardCopyOption.COPY_ATTRIBUTES);
                    }
                    return CONTINUE;
                }
            });
    return totalSize.get();
}

From source file:org.sonar.application.AppFileSystem.java

private File createOrCleanTempDirectory(String propKey) throws IOException {
    File dir = settings.getProps().nonNullValueAsFile(propKey);
    LOG.info("Cleaning or creating temp directory {}", dir.getAbsolutePath());
    if (!createDirectory(propKey)) {
        Files.walkFileTree(dir.toPath(), FOLLOW_LINKS, CleanTempDirFileVisitor.VISIT_MAX_DEPTH,
                new CleanTempDirFileVisitor(dir.toPath()));
    }//from ww w  .j a  v a2  s  .  com
    return dir;
}

From source file:com.netflix.genie.common.internal.dto.JobDirectoryManifest.java

/**
 * Create a manifest from the given job directory.
 *
 * @param directory              The job directory to create a manifest from
 * @param calculateFileChecksums Whether or not to calculate checksums for each file added to the manifest
 * @throws IOException If there is an error reading the directory
 *///from  ww  w  .ja va2 s.  c o m
public JobDirectoryManifest(final Path directory, final boolean calculateFileChecksums) throws IOException {
    // Walk the directory
    final ImmutableMap.Builder<String, ManifestEntry> builder = ImmutableMap.builder();
    final ManifestVisitor manifestVisitor = new ManifestVisitor(directory, builder, calculateFileChecksums);
    final EnumSet<FileVisitOption> options = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
    Files.walkFileTree(directory, options, Integer.MAX_VALUE, manifestVisitor);
    this.entries = builder.build();

    final ImmutableSet.Builder<ManifestEntry> filesBuilder = ImmutableSet.builder();
    final ImmutableSet.Builder<ManifestEntry> directoriesBuilder = ImmutableSet.builder();

    long sizeOfFiles = 0L;
    for (final ManifestEntry entry : this.entries.values()) {
        if (entry.isDirectory()) {
            directoriesBuilder.add(entry);
        } else {
            filesBuilder.add(entry);
            sizeOfFiles += entry.getSize();
        }
    }

    this.totalSizeOfFiles = sizeOfFiles;
    this.directories = directoriesBuilder.build();
    this.files = filesBuilder.build();
    this.numDirectories = this.directories.size();
    this.numFiles = this.files.size();
}

From source file:net.mozq.picto.core.ProcessCore.java

public static void findFiles(ProcessCondition processCondition, Consumer<ProcessData> processDataSetter,
        BooleanSupplier processStopper) throws IOException {

    Set<FileVisitOption> fileVisitOptionSet;
    if (processCondition.isFollowLinks()) {
        fileVisitOptionSet = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
    } else {//www .  ja va 2 s.  co  m
        fileVisitOptionSet = Collections.emptySet();
    }

    Files.walkFileTree(processCondition.getSrcRootPath(), fileVisitOptionSet, processCondition.getDept(),
            new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
                    if (processStopper.getAsBoolean()) {
                        return FileVisitResult.TERMINATE;
                    }

                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

                    if (attrs.isDirectory()) {
                        return FileVisitResult.SKIP_SUBTREE;
                    }

                    if (processStopper.getAsBoolean()) {
                        return FileVisitResult.TERMINATE;
                    }

                    if (!processCondition.getPathFilter().accept(file, attrs)) {
                        return FileVisitResult.SKIP_SUBTREE;
                    }

                    Path rootRelativeSubPath = processCondition.getSrcRootPath().relativize(file.getParent());

                    ImageMetadata imageMetadata = getImageMetadata(file);

                    Date baseDate;
                    if (processCondition.isChangeFileCreationDate()
                            || processCondition.isChangeFileModifiedDate()
                            || processCondition.isChangeFileAccessDate()
                            || processCondition.isChangeExifDate()) {
                        baseDate = getBaseDate(processCondition, file, attrs, imageMetadata);
                    } else {
                        baseDate = null;
                    }

                    String destSubPathname = processCondition.getDestSubPathFormat().format(varName -> {
                        try {
                            switch (varName) {
                            case "Now":
                                return new Date();
                            case "ParentSubPath":
                                return rootRelativeSubPath.toString();
                            case "FileName":
                                return file.getFileName().toString();
                            case "BaseName":
                                return FileUtilz.getBaseName(file.getFileName().toString());
                            case "Extension":
                                return FileUtilz.getExt(file.getFileName().toString());
                            case "Size":
                                return Long.valueOf(Files.size(file));
                            case "CreationDate":
                                return (processCondition.isChangeFileCreationDate()) ? baseDate
                                        : new Date(attrs.creationTime().toMillis());
                            case "ModifiedDate":
                                return (processCondition.isChangeFileModifiedDate()) ? baseDate
                                        : new Date(attrs.lastModifiedTime().toMillis());
                            case "AccessDate":
                                return (processCondition.isChangeFileAccessDate()) ? baseDate
                                        : new Date(attrs.lastAccessTime().toMillis());
                            case "PhotoTakenDate":
                                return (processCondition.isChangeExifDate()) ? baseDate
                                        : getPhotoTakenDate(file, imageMetadata);
                            case "Width":
                                return getEXIFIntValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_EXIF_IMAGE_WIDTH);
                            case "Height":
                                return getEXIFIntValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_EXIF_IMAGE_LENGTH);
                            case "FNumber":
                                return getEXIFDoubleValue(imageMetadata, ExifTagConstants.EXIF_TAG_FNUMBER);
                            case "Aperture":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_APERTURE_VALUE);
                            case "MaxAperture":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_MAX_APERTURE_VALUE);
                            case "ISO":
                                return getEXIFIntValue(imageMetadata, ExifTagConstants.EXIF_TAG_ISO);
                            case "FocalLength":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_FOCAL_LENGTH); // ?
                            case "FocalLength35mm":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_FOCAL_LENGTH_IN_35MM_FORMAT);
                            case "ShutterSpeed":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_SHUTTER_SPEED_VALUE);
                            case "Exposure":
                                return getEXIFStringValue(imageMetadata, ExifTagConstants.EXIF_TAG_EXPOSURE); // 
                            case "ExposureTime":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_EXPOSURE_TIME); // 
                            case "ExposureMode":
                                return getEXIFIntValue(imageMetadata, ExifTagConstants.EXIF_TAG_EXPOSURE_MODE);
                            case "ExposureProgram":
                                return getEXIFIntValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_EXPOSURE_PROGRAM);
                            case "Brightness":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_BRIGHTNESS_VALUE);
                            case "WhiteBalance":
                                return getEXIFIntValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_WHITE_BALANCE_1);
                            case "LightSource":
                                return getEXIFIntValue(imageMetadata, ExifTagConstants.EXIF_TAG_LIGHT_SOURCE);
                            case "Lens":
                                return getEXIFStringValue(imageMetadata, ExifTagConstants.EXIF_TAG_LENS);
                            case "LensMake":
                                return getEXIFStringValue(imageMetadata, ExifTagConstants.EXIF_TAG_LENS_MAKE);
                            case "LensModel":
                                return getEXIFStringValue(imageMetadata, ExifTagConstants.EXIF_TAG_LENS_MODEL);
                            case "LensSerialNumber":
                                return getEXIFStringValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_LENS_SERIAL_NUMBER);
                            case "Make":
                                return getEXIFStringValue(imageMetadata, TiffTagConstants.TIFF_TAG_MAKE);
                            case "Model":
                                return getEXIFStringValue(imageMetadata, TiffTagConstants.TIFF_TAG_MODEL);
                            case "SerialNumber":
                                return getEXIFStringValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_SERIAL_NUMBER);
                            case "Software":
                                return getEXIFStringValue(imageMetadata, ExifTagConstants.EXIF_TAG_SOFTWARE);
                            case "ProcessingSoftware":
                                return getEXIFStringValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_PROCESSING_SOFTWARE);
                            case "OwnerName":
                                return getEXIFStringValue(imageMetadata, ExifTagConstants.EXIF_TAG_OWNER_NAME);
                            case "CameraOwnerName":
                                return getEXIFStringValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_CAMERA_OWNER_NAME);
                            case "GPSLat":
                                return getEXIFGpsLat(imageMetadata);
                            case "GPSLatDeg":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE,
                                        0);
                            case "GPSLatMin":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE,
                                        1);
                            case "GPSLatSec":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE,
                                        2);
                            case "GPSLatRef":
                                return getEXIFStringValue(imageMetadata,
                                        GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF);
                            case "GPSLon":
                                return getEXIFGpsLon(imageMetadata);
                            case "GPSLonDeg":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE,
                                        0);
                            case "GPSLonMin":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE,
                                        1);
                            case "GPSLonSec":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE,
                                        2);
                            case "GPSLonRef":
                                return getEXIFStringValue(imageMetadata,
                                        GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
                            case "GPSAlt":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_ALTITUDE);
                            case "GPSAltRef":
                                return getEXIFIntValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_ALTITUDE_REF);
                            default:
                                throw new PictoInvalidDestinationPathException(Messages
                                        .getString("message.warn.invalid.destSubPath.varName", varName));
                            }
                        } catch (PictoException e) {
                            throw e;
                        } catch (Exception e) {
                            throw new PictoInvalidDestinationPathException(
                                    Messages.getString("message.warn.invalid.destSubPath.pattern"), e);
                        }
                    });

                    Path destSubPath = processCondition.getDestRootPath().resolve(destSubPathname).normalize();

                    if (!destSubPath.startsWith(processCondition.getDestRootPath())) {
                        throw new PictoInvalidDestinationPathException(
                                Messages.getString("message.warn.invalid.destination.path", destSubPath));
                    }

                    ProcessData processData = new ProcessData();
                    processData.setSrcPath(file);
                    processData.setSrcFileAttributes(attrs);
                    processData.setDestPath(destSubPath);
                    processData.setBaseDate(baseDate);

                    processDataSetter.accept(processData);

                    return FileVisitResult.CONTINUE;
                }
            });
}

From source file:org.tinymediamanager.core.movie.tasks.MovieFindMissingTask.java

public HashSet<Path> getBigFilesRecursive(Path folder) {
    BigFilesRecursive visitor = new BigFilesRecursive();
    try {/*from  w  ww. j  ava2s.c om*/
        Files.walkFileTree(folder, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, visitor);
    } catch (IOException e) {
        // can not happen, since we overrided visitFileFailed, which throws no exception ;)
    }
    return visitor.fFound;
}

From source file:com.amazonaws.codepipeline.jenkinsplugin.CompressionTools.java

public static List<File> addFilesToCompress(final Path pathToCompress, final BuildListener listener)
        throws IOException {
    final List<File> files = new ArrayList<>();

    if (pathToCompress != null) {
        Files.walkFileTree(pathToCompress, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                new SimpleFileVisitor<Path>() {
                    @Override//ww w  .j  a va  2 s .  c o m
                    public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
                            throws IOException {
                        files.add(file.toFile());
                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult visitFileFailed(final Path file, final IOException e)
                            throws IOException {
                        if (e != null) {
                            LoggingHelper.log(listener, "Failed to visit file '%s'. Error: %s.",
                                    file.toString(), e.getMessage());
                            LoggingHelper.log(listener, e);
                            throw e;
                        }
                        return FileVisitResult.CONTINUE;
                    }
                });
    }

    return files;
}

From source file:com.kappaware.logtrawler.DirWatcher.java

private void registerWithSub(final Path path, FileEvent.Type newFiletype) throws IOException {
    Files.walkFileTree(path, this.fileVisitOptions, Integer.MAX_VALUE, new MyPathVisitor(newFiletype));
}

From source file:de.ingrid.interfaces.csw.cache.AbstractFileCache.java

/**
 * Get document ids from a directory and all sub directories
 * /*from  ww w  . j  ava 2 s .c  om*/
 * @param directory
 *            The start directory
 * @return Set
 */
protected Set<Serializable> getDocumentIds(File directory) {
    Set<Serializable> documentIds = new HashSet<Serializable>();

    EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
    CacheFileLister cfl = new CacheFileLister(documentIds);
    try {
        Files.walkFileTree(directory.toPath(), opts, Integer.MAX_VALUE, cfl);
    } catch (IOException e) {
        log.error("Error getting document IDs from cache.");
    }

    return documentIds;
}