Search in sources :

Example 16 with FileVisitResult

use of java.nio.file.FileVisitResult in project jdk8u_jdk by JetBrains.

the class Test method deleteGeneratedFiles.

private static void deleteGeneratedFiles() {
    Path p = Paths.get("..", "classes", "javax", "xml", "ws", "xsanymixed", "org");
    System.out.println("performing cleanup, deleting wsdl compilation result: " + p.toFile().getAbsolutePath());
    if (Files.exists(p)) {
        try {
            Files.walkFileTree(p, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    System.out.println("deleting file [" + file.toFile().getAbsoluteFile() + "]");
                    Files.delete(file);
                    return CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    System.out.println("deleting dir [" + dir.toFile().getAbsoluteFile() + "]");
                    if (exc == null) {
                        Files.delete(dir);
                        return CONTINUE;
                    } else {
                        throw exc;
                    }
                }
            });
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 17 with FileVisitResult

use of java.nio.file.FileVisitResult in project OpenAM by OpenRock.

the class DefaultDebugRecorder method delete.

/**
     * Delete a file
     *
     * @param file to delete
     * @throws IOException
     */
private void delete(String file) throws IOException {
    Path start = Paths.get(file);
    Files.walkFileTree(start, 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 {
                // directory iteration failed
                throw e;
            }
        }
    });
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 18 with FileVisitResult

use of java.nio.file.FileVisitResult in project OpenAM by OpenRock.

the class ZipUtils method generateZip.

/**
     * Generate a zip
     *
     * Due to a bug in Java 7 corrected in Java 8 http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7156873
     * srcFolder and outputZip can't be URL encoded if you're using java 7.
     *
     * @param srcFolder source folder
     * @param outputZip zip folder
     * @return the list of files that were included in the archive.
     * @throws IOException if an error occurs creating the zip archive.
     * @throws URISyntaxException if an error occurs creating the zip archive.
     */
public static List<String> generateZip(String srcFolder, String outputZip) throws IOException, URISyntaxException {
    final Path targetZip = Paths.get(outputZip);
    final Path sourceDir = Paths.get(srcFolder);
    final URI uri = new URI("jar", URLDecoder.decode(targetZip.toUri().toString(), "UTF-8"), null);
    final List<String> files = new ArrayList<>();
    try (FileSystem zipfs = FileSystems.newFileSystem(uri, singletonMap("create", "true"))) {
        Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                // In case the target zip is being created in the folder being zipped (e.g. Fedlet), ignore it.
                if (targetZip.equals(file)) {
                    return FileVisitResult.CONTINUE;
                }
                Path target = zipfs.getPath(sourceDir.relativize(file).toString());
                if (target.getParent() != null) {
                    Files.createDirectories(target.getParent());
                }
                Files.copy(file, target, StandardCopyOption.REPLACE_EXISTING);
                files.add(file.toString());
                return FileVisitResult.CONTINUE;
            }
        });
    }
    return files;
}
Also used : Path(java.nio.file.Path) FileSystem(java.nio.file.FileSystem) ArrayList(java.util.ArrayList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) URI(java.net.URI) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 19 with FileVisitResult

use of java.nio.file.FileVisitResult in project asterixdb by apache.

the class LocalFileSystemUtils method traverse.

public static void traverse(final List<File> files, File root, final String expression, final LinkedList<Path> dirs) throws IOException {
    final Path path = root.toPath();
    if (!Files.exists(path)) {
        throw new RuntimeDataException(ErrorCode.UTIL_LOCAL_FILE_SYSTEM_UTILS_PATH_NOT_FOUND, path.toString());
    }
    if (!Files.isDirectory(path)) {
        validateAndAdd(path, expression, files);
    }
    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs) throws IOException {
            if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
                return FileVisitResult.TERMINATE;
            }
            if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
                if (dirs != null) {
                    dirs.add(path);
                }
                //get immediate children files
                File[] content = path.toFile().listFiles();
                for (File file : content) {
                    if (!file.isDirectory()) {
                        validateAndAdd(file.toPath(), expression, files);
                    }
                }
            } else {
                // Path is a file, add to list of files if it matches the expression
                validateAndAdd(path, expression, files);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 20 with FileVisitResult

use of java.nio.file.FileVisitResult in project beam by apache.

the class ApexYarnLauncher method createJar.

/**
   * Create a jar file from the given directory.
   * @param dir source directory
   * @param jarFile jar file name
   * @throws IOException when file cannot be created
   */
public static void createJar(File dir, File jarFile) throws IOException {
    final Map<String, ?> env = Collections.singletonMap("create", "true");
    if (jarFile.exists() && !jarFile.delete()) {
        throw new RuntimeException("Failed to remove " + jarFile);
    }
    URI uri = URI.create("jar:" + jarFile.toURI());
    try (final FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
        File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
        Files.createDirectory(zipfs.getPath("META-INF"));
        try (final OutputStream out = Files.newOutputStream(zipfs.getPath(JarFile.MANIFEST_NAME))) {
            if (!manifestFile.exists()) {
                new Manifest().write(out);
            } else {
                FileUtils.copyFile(manifestFile, out);
            }
        }
        final java.nio.file.Path root = dir.toPath();
        Files.walkFileTree(root, new java.nio.file.SimpleFileVisitor<Path>() {

            String relativePath;

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                relativePath = root.relativize(dir).toString();
                if (!relativePath.isEmpty()) {
                    if (!relativePath.endsWith("/")) {
                        relativePath += "/";
                    }
                    if (!relativePath.equals("META-INF/")) {
                        final Path dstDir = zipfs.getPath(relativePath);
                        Files.createDirectory(dstDir);
                    }
                }
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String name = relativePath + file.getFileName();
                if (!JarFile.MANIFEST_NAME.equals(name)) {
                    try (final OutputStream out = Files.newOutputStream(zipfs.getPath(name))) {
                        FileUtils.copyFile(file.toFile(), out);
                    }
                }
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                relativePath = root.relativize(dir.getParent()).toString();
                if (!relativePath.isEmpty() && !relativePath.endsWith("/")) {
                    relativePath += "/";
                }
                return super.postVisitDirectory(dir, exc);
            }
        });
    }
}
Also used : Path(java.nio.file.Path) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) URI(java.net.URI) FileSystem(java.nio.file.FileSystem) JarFile(java.util.jar.JarFile) File(java.io.File) Path(java.nio.file.Path) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

FileVisitResult (java.nio.file.FileVisitResult)74 Path (java.nio.file.Path)67 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)66 IOException (java.io.IOException)65 File (java.io.File)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 InputStream (java.io.InputStream)4 HashSet (java.util.HashSet)4 JarEntry (java.util.jar.JarEntry)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 FileOutputStream (java.io.FileOutputStream)3 JarOutputStream (java.util.jar.JarOutputStream)3 PathSourcePath (com.facebook.buck.rules.PathSourcePath)2 SourcePath (com.facebook.buck.rules.SourcePath)2 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)2 FileSystemException (io.vertx.core.file.FileSystemException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2