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;
}
});
}
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);
}
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;
}
});
}
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();
}
Aggregations