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