Java Path Copy nio copyContainer(String from, String to, IProgressMonitor monitor)

Here you can find the source of copyContainer(String from, String to, IProgressMonitor monitor)

Description

copy Container

License

Apache License

Declaration

public static void copyContainer(String from, String to, IProgressMonitor monitor) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;

import java.util.EnumSet;

import org.eclipse.core.runtime.IProgressMonitor;

public class Main {
    public static void copyContainer(String from, String to, IProgressMonitor monitor) throws IOException {
        Path fromPath = Paths.get(from);
        Path toPath = Paths.get(to);
        Path bakPath = null;/*from w  w  w  . java 2  s . co m*/
        monitor.beginTask("Copy to target", 100);
        if (Files.exists(toPath)) {
            bakPath = toPath.getParent().resolve(toPath.getFileName().toString() + ".bak");
            deleteDir(bakPath);
            Files.move(toPath, bakPath, StandardCopyOption.REPLACE_EXISTING);
        }

        Files.createDirectories(toPath);

        try {
            copyDir(fromPath, toPath, monitor);
        } catch (IOException e) {
            deleteDir(toPath);
            if (bakPath != null) {
                Files.move(bakPath, toPath, StandardCopyOption.REPLACE_EXISTING);
            }
            throw e;
        } finally {
            if (bakPath != null) {
                deleteDir(bakPath);
            }
            monitor.done();
        }
    }

    private static void deleteDir(Path dir) throws IOException {
        if (!Files.exists(dir)) {
            return;
        }

        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
                if (e == null) {
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                } else {
                    throw e;
                }
            }
        });
    }

    private static void copyDir(Path from, Path to, IProgressMonitor monitor) throws IOException {
        Files.walkFileTree(from, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                new SimpleFileVisitor<Path>() {

                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                            throws IOException {
                        Path targetdir = to.resolve(from.relativize(dir));
                        try {
                            Files.copy(dir, targetdir);
                            monitor.subTask(dir.toString());
                            monitor.worked(1);
                        } catch (FileAlreadyExistsException e) {
                            if (!Files.isDirectory(targetdir))
                                throw e;
                        }
                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        Files.copy(file, to.resolve(from.relativize(file)));
                        monitor.subTask(file.toString());
                        monitor.worked(1);
                        return FileVisitResult.CONTINUE;
                    }
                });
    }
}

Related

  1. copy(Path src, Path dest, List globPattern)
  2. copy(String filePath, String fileName)
  3. copyAlways(Path source, Path target)
  4. copyAsTrueTempFile(Path source, FileAttribute... attrs)
  5. copyConfigFile(final String file, final Path dir)
  6. copyContents(Path from, Path to)
  7. copyEverythingExcept(Path file, Path srcDir, Path dstDir, Predicate excluded, Consumer onCopy)
  8. copyFile(final String fileName, final String from, final String to)
  9. copyFile(InputStream from, Path to)