Example usage for java.nio.file FileVisitResult CONTINUE

List of usage examples for java.nio.file FileVisitResult CONTINUE

Introduction

In this page you can find the example usage for java.nio.file FileVisitResult CONTINUE.

Prototype

FileVisitResult CONTINUE

To view the source code for java.nio.file FileVisitResult CONTINUE.

Click Source Link

Document

Continue.

Usage

From source file:com.cloudbees.clickstack.util.Files2.java

public static void unzip(@Nonnull Path zipFile, @Nonnull final Path destDir) throws RuntimeIOException {
    try {//from www  . jav  a2  s . co  m
        //if the destination doesn't exist, create it
        if (Files.notExists(destDir)) {
            logger.trace("Create dir: {}", destDir);
            Files.createDirectories(destDir);
        }

        try (FileSystem zipFileSystem = createZipFileSystem(zipFile, false)) {
            final Path root = zipFileSystem.getPath("/");

            //walk the zip file tree and copy files to the destination
            Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    try {
                        final Path destFile = Paths.get(destDir.toString(), file.toString());
                        logger.trace("Extract file {} to {}", file, destDir);
                        Files.copy(file, destFile, StandardCopyOption.REPLACE_EXISTING);
                    } catch (IOException | RuntimeException e) {
                        logger.warn("Exception copying file '" + file + "' to '" + destDir + "', ignore file",
                                e);
                    }
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
                    final Path dirToCreate = Paths.get(destDir.toString(), dir.toString());

                    if (Files.notExists(dirToCreate)) {
                        logger.trace("Create dir {}", dirToCreate);
                        try {
                            Files.createDirectory(dirToCreate);
                        } catch (IOException e) {
                            logger.warn("Exception creating directory '" + dirToCreate + "' for '" + dir
                                    + "', ignore dir subtree", e);
                            return FileVisitResult.SKIP_SUBTREE;
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    } catch (IOException e) {
        throw new RuntimeIOException("Exception expanding " + zipFile + " to " + destDir, e);
    }
}

From source file:it.polimi.diceH2020.launcher.Experiment.java

void wipeResultDir() throws IOException {
    Path result = Paths.get(settings.getResultDir());
    if (Files.exists(result)) {
        Files.walkFileTree(result, new SimpleFileVisitor<Path>() {
            @Override//from  w  w  w.jav  a 2 s .  c  om
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }

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

From source file:gov.pnnl.goss.gridappsd.app.AppManagerImpl.java

@Override
public void deRegisterApp(String appId) {
    appId = appId.trim();/*from   ww w. ja v  a2s  . com*/
    // find and stop any running instances
    stopApp(appId);

    // remove app from mapping
    apps.remove(appId);

    // get app directory from config and remove files for app_id
    File configDir = getAppConfigDirectory();
    File appDir = new File(configDir.getAbsolutePath() + File.separator + appId);
    try {
        Files.walkFileTree(appDir.toPath(), new FileVisitor<Path>() {
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                if (dir.toFile().delete()) {
                    return FileVisitResult.CONTINUE;
                } else {
                    return FileVisitResult.TERMINATE;
                }
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                return FileVisitResult.CONTINUE;
            }

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

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.TERMINATE;
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }

    appDir.delete();

    File appInfoFile = new File(configDir.getAbsolutePath() + File.separator + appId + CONFIG_FILE_EXT);
    appInfoFile.delete();

}

From source file:com.rover12421.shaka.apktool.lib.AndrolibAj.java

/**
 * @param joinPoint/*  w w  w  . j a  va2  s  .c om*/
 * @param apkFile
 * @param outDir
 * @param resTable
 */
@After("execution(* brut.androlib.Androlib.decodeUnknownFiles(..))" + "&& args(apkFile, outDir, resTable)")
public void decodeUnknownFiles_after(JoinPoint joinPoint, ExtFile apkFile, File outDir, ResTable resTable) {
    try {
        File unknownOut = new File(outDir, getUNK_DIRNAME());
        ResUnknownFiles mResUnknownFiles = Reflect.on(joinPoint.getThis()).get("mResUnknownFiles");

        Directory unk = apkFile.getDirectory();
        // loop all items in container recursively, ignoring any that are pre-defined by aapt
        Set<String> files = unk.getFiles(true);
        for (String file : files) {
            if (file.equals("classes.dex") || file.equals("resources.arsc")) {
                continue;
            }

            /**
             * ??
             */
            if (file.replaceFirst("META-INF[/\\\\]+[^/\\\\]+\\.(SF|RSA)", "").isEmpty()) {
                continue;
            }

            /**
             * dex
             */
            if (DexMaps.containsKey(file)) {
                continue;
            }

            String decodeMapFileName = getDecodeFileMapName(file);
            File resFile = new File(outDir, decodeMapFileName);
            if (resFile.exists()) {
                //??,
                mResUnknownFiles.getUnknownFiles().remove(file);
                File needDeleteFile = new File(unknownOut, file);
                if (needDeleteFile.exists()) {
                    //?,?
                    needDeleteFile.delete();
                }
            } else {
                File unFile = new File(unknownOut, file);
                if (unFile.exists()) {
                    //?
                    continue;
                }

                //?,
                // copy file out of archive into special "unknown" folder
                unk.copyToDir(unknownOut, file);
                // lets record the name of the file, and its compression type
                // so that we may re-include it the same way
                mResUnknownFiles.addUnknownFileInfo(file, String.valueOf(unk.getCompressionLevel(file)));
            }

        }

        if (unknownOut.exists()) {
            //
            Files.walkFileTree(Paths.get(unknownOut.getAbsolutePath()), new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    try {
                        Files.deleteIfExists(dir);
                    } catch (Exception e) {
                        // ignore exception
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.liferay.sync.engine.document.library.handler.GetSyncDLObjectUpdateHandler.java

protected void deleteFile(SyncFile sourceSyncFile, SyncFile targetSyncFile) throws Exception {

    if (sourceSyncFile.getUiEvent() == SyncFile.UI_EVENT_DELETED_LOCAL) {
        return;/*from w  w w . j  a  va 2  s.c  om*/
    }

    sourceSyncFile.setModifiedTime(targetSyncFile.getModifiedTime());

    String event = targetSyncFile.getEvent();

    if (event.equals(SyncFile.EVENT_TRASH)) {
        sourceSyncFile.setUiEvent(SyncFile.UI_EVENT_TRASHED_REMOTE);
    } else {
        sourceSyncFile.setUiEvent(SyncFile.UI_EVENT_DELETED_REMOTE);
    }

    sourceSyncFile.setUserId(targetSyncFile.getUserId());
    sourceSyncFile.setUserName(targetSyncFile.getUserName());

    if (!sourceSyncFile.isUnsynced()) {
        SyncFileService.deleteSyncFile(sourceSyncFile);
    }

    Path sourceFilePath = Paths.get(sourceSyncFile.getFilePathName());

    if (FileUtil.notExists(sourceFilePath)) {
        return;
    }

    final Watcher watcher = WatcherManager.getWatcher(getSyncAccountId());

    if (sourceSyncFile.isFile()) {
        watcher.addDeletedFilePathName(sourceSyncFile.getFilePathName());

        FileUtil.deleteFile(sourceFilePath);

        return;
    }

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

        @Override
        public FileVisitResult postVisitDirectory(Path filePath, IOException ioe) throws IOException {

            if (ioe != null) {
                return super.postVisitDirectory(filePath, ioe);
            }

            watcher.addDeletedFilePathName(filePath.toString());

            FileUtil.deleteFile(filePath);

            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult preVisitDirectory(Path filePath, BasicFileAttributes basicFileAttributes)
                throws IOException {

            watcher.unregisterFilePath(filePath);

            return super.preVisitDirectory(filePath, basicFileAttributes);
        }

        @Override
        public FileVisitResult visitFile(Path filePath, BasicFileAttributes basicFileAttributes)
                throws IOException {

            watcher.addDeletedFilePathName(filePath.toString());

            FileUtil.deleteFile(filePath);

            return FileVisitResult.CONTINUE;
        }

    });
}

From source file:com.facebook.buck.io.filesystem.impl.DefaultProjectFilesystemTest.java

@Test
public void testWalkFileTreeFollowsSymlinks() throws IOException {
    tmp.newFolder("dir");
    tmp.newFile("dir/file.txt");
    CreateSymlinksForTests.createSymLink(tmp.getRoot().resolve("linkdir"), tmp.getRoot().resolve("dir"));

    ImmutableList.Builder<Path> filePaths = ImmutableList.builder();

    filesystem.walkRelativeFileTree(Paths.get(""), new SimpleFileVisitor<Path>() {
        @Override/* ww w .  java 2s  .  c om*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            filePaths.add(file);
            return FileVisitResult.CONTINUE;
        }
    });

    assertThat(filePaths.build(), containsInAnyOrder(Paths.get("dir/file.txt"), Paths.get("linkdir/file.txt")));
}

From source file:dk.dma.ais.downloader.QueryService.java

/**
 * called every hour to clean up the repo
 *///from   www .j a v a2s  .  c o m
@Scheduled(cron = "12 27 */1 * * *")
public void cleanUpRepoFolder() {

    long now = System.currentTimeMillis();
    long expiredTime = now - FILE_EXPIRY_MS;

    try {
        Files.walkFileTree(getRepoRoot(), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                if (!dir.equals(getRepoRoot()) && isDirEmpty(dir)) {
                    log.info("Deleting repo directory :" + dir);
                    Files.delete(dir);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (Files.getLastModifiedTime(file).toMillis() < expiredTime) {
                    log.info("Deleting repo file      :" + file);
                    Files.delete(file);
                }
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        log.log(Level.SEVERE, "Failed cleaning up repo: " + e.getMessage());
    }

    log.info(String.format("Cleaned up repo in %d ms", System.currentTimeMillis() - now));
}

From source file:com.nridge.connector.fs.con_fs.core.FileCrawler.java

/**
 * Invoked for a file in a directory./*  www.  j  a va 2s  .  c  om*/
 * Unless overridden, this method returns {@link java.nio.file.FileVisitResult#CONTINUE
 * CONTINUE}.
 *
 * @param aPath Path instance.
 * @param aFileAttributes File attribute instance.
 */
@Override
public FileVisitResult visitFile(Path aPath, BasicFileAttributes aFileAttributes) throws IOException {
    Logger appLogger = mAppMgr.getLogger(this, "visitFile");

    String pathFileName = aPath.toAbsolutePath().toString();
    if (mCrawlIgnore.isMatchedNormalized(pathFileName))
        appLogger.debug(String.format("Ignoring File: %s", pathFileName));
    else {
        File fsFile = aPath.toFile();
        if ((fsFile.canRead()) && (mBag != null)) {
            String crawlType = mCrawlQueue.getCrawlType();
            if (StringUtils.equals(crawlType, Connector.CRAWL_TYPE_INCREMENTAL)) {
                String docId = generateDocumentId(aPath);
                boolean docExistsInIndex = documentExistsInIndex(docId);
                if (docExistsInIndex) {
                    Date incDate = mCrawlQueue.getCrawlLastModified();
                    FileTime lastModifiedTime = aFileAttributes.lastModifiedTime();
                    Date lmDate = new Date(lastModifiedTime.toMillis());
                    if (lmDate.after(incDate))
                        processFile(aPath, aFileAttributes);
                } else
                    processFile(aPath, aFileAttributes);
            } else
                processFile(aPath, aFileAttributes);
        } else
            appLogger.warn(String.format("Access Failed: %s", pathFileName));
    }

    if (mAppMgr.isAlive())
        return FileVisitResult.CONTINUE;
    else
        return FileVisitResult.TERMINATE;
}

From source file:org.schedulesdirect.grabber.Grabber.java

private void loadSeriesInfoIds(Path root) throws IOException {
    Files.walkFileTree(root, new FileVisitor<Path>() {
        @Override/*from  ww w.  j a v  a2 s.co m*/
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            return FileVisitResult.SKIP_SUBTREE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            String name = file.getFileName().toString();
            name = name.substring(0, name.lastIndexOf('.'));
            cachedSeriesIds.add(name);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
            throw exc;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            if (exc != null)
                throw exc;
            return FileVisitResult.CONTINUE;
        }
    });
}