Search in sources :

Example 71 with FileVisitResult

use of java.nio.file.FileVisitResult in project cdap by caskdata.

the class BundleJarUtil method addToArchive.

/**
   * Adds file(s) to a zip archive. If the given input file is a directory,
   * all files under it will be added recursively.
   *
   * @param input input directory (or file) whose contents needs to be archived
   * @param output an opened {@link ZipOutputStream} for the archive content to add to
   * @throws IOException if there is failure in the archive creation
   */
public static void addToArchive(File input, final ZipOutputStream output) throws IOException {
    final URI baseURI = input.toURI();
    Files.walkFileTree(input.toPath(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            URI uri = baseURI.relativize(dir.toUri());
            if (!uri.getPath().isEmpty()) {
                output.putNextEntry(new ZipEntry(uri.getPath()));
                output.closeEntry();
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            URI uri = baseURI.relativize(file.toUri());
            if (uri.getPath().isEmpty()) {
                // Only happen if the given "input" is a file.
                output.putNextEntry(new ZipEntry(file.toFile().getName()));
            } else {
                output.putNextEntry(new ZipEntry(uri.getPath()));
            }
            Files.copy(file, output);
            output.closeEntry();
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) URI(java.net.URI) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 72 with FileVisitResult

use of java.nio.file.FileVisitResult in project bndtools by bndtools.

the class ImportBndWorkspaceWizard method deleteOldProjectFiles.

private void deleteOldProjectFiles(final Path projectPath) throws IOException {
    final Path settings = projectPath.resolve(".settings");
    if (Files.exists(settings)) {
        Files.walkFileTree(settings, 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 exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    }
    final Path project = projectPath.resolve(".project");
    Files.deleteIfExists(project);
    final Path classpath = projectPath.resolve(".classpath");
    Files.deleteIfExists(classpath);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 73 with FileVisitResult

use of java.nio.file.FileVisitResult in project ceylon-compiler by ceylon.

the class JavacPathFileManager method list.

private void list(Path path, String packageName, final Set<Kind> kinds, boolean recurse, final ListBuffer<JavaFileObject> results) throws IOException {
    if (!Files.exists(path))
        return;
    final Path pathDir;
    if (isDirectory(path))
        pathDir = path;
    else {
        FileSystem fs = getFileSystem(path);
        if (fs == null)
            return;
        pathDir = fs.getRootDirectories().iterator().next();
    }
    String sep = path.getFileSystem().getSeparator();
    Path packageDir = packageName.isEmpty() ? pathDir : pathDir.resolve(packageName.replace(".", sep));
    if (!Files.exists(packageDir))
        return;
    /* Alternate impl of list, superceded by use of Files.walkFileTree */
    //        Deque<Path> queue = new LinkedList<Path>();
    //        queue.add(packageDir);
    //
    //        Path dir;
    //        while ((dir = queue.poll()) != null) {
    //            DirectoryStream<Path> ds = dir.newDirectoryStream();
    //            try {
    //                for (Path p: ds) {
    //                    String name = p.getFileName().toString();
    //                    if (isDirectory(p)) {
    //                        if (recurse && SourceVersion.isIdentifier(name)) {
    //                            queue.add(p);
    //                        }
    //                    } else {
    //                        if (kinds.contains(getKind(name))) {
    //                            JavaFileObject fe =
    //                                PathFileObject.createDirectoryPathFileObject(this, p, pathDir);
    //                            results.append(fe);
    //                        }
    //                    }
    //                }
    //            } finally {
    //                ds.close();
    //            }
    //        }
    int maxDepth = (recurse ? Integer.MAX_VALUE : 1);
    Set<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS);
    Files.walkFileTree(packageDir, opts, maxDepth, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
            Path name = dir.getFileName();
            if (// JSR 292?
            name == null || SourceVersion.isIdentifier(name.toString()))
                return FileVisitResult.CONTINUE;
            else
                return FileVisitResult.SKIP_SUBTREE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            if (attrs.isRegularFile() && kinds.contains(getKind(file.getFileName().toString()))) {
                JavaFileObject fe = PathFileObject.createDirectoryPathFileObject(JavacPathFileManager.this, file, pathDir);
                results.append(fe);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) JavaFileObject(javax.tools.JavaFileObject) FileVisitOption(java.nio.file.FileVisitOption) FileSystem(java.nio.file.FileSystem) FileVisitResult(java.nio.file.FileVisitResult) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 74 with FileVisitResult

use of java.nio.file.FileVisitResult in project ceylon-compiler by ceylon.

the class CeylonDocToolTests method makeCarFromClassFiles.

private void makeCarFromClassFiles(File dir, String[] fileNames, String module, String version) throws IOException {
    String modulePath = module.replace('.', '/') + "/";
    final Path dirPath = dir.toPath();
    File classFolder = new File(dir, modulePath);
    File jarFolder = new File(dir, modulePath + version);
    jarFolder.mkdirs();
    File jarFile = new File(jarFolder, module + "-" + version + ".car");
    // now jar it up
    JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(jarFile));
    final List<String> files = new LinkedList<String>();
    final Path classPath = classFolder.toPath();
    // collect all class files
    Files.walkFileTree(classPath, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path path, BasicFileAttributes attr) throws IOException {
            if (path.toString().endsWith(".class")) {
                files.add(dirPath.relativize(path).toString());
            }
            return FileVisitResult.CONTINUE;
        }
    });
    for (String classFile : files) {
        ZipEntry entry = new ZipEntry(classFile);
        outputStream.putNextEntry(entry);
        File javaFile = new File(dir, classFile);
        FileInputStream inputStream = new FileInputStream(javaFile);
        com.redhat.ceylon.compiler.java.util.Util.copy(inputStream, outputStream);
        inputStream.close();
        outputStream.flush();
    }
    outputStream.close();
}
Also used : Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) 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