List of usage examples for java.nio.file Files walkFileTree
public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException
From source file:com.searchcode.app.jobs.IndexSvnRepoJob.java
/** * Indexes all the documents in the path provided. Will also remove anything from the index if not on disk * Generally this is a slow update used only for the inital clone of a repository * NB this can be used for updates but it will be much slower as it needs to to walk the contents of the disk */// w ww. ja va2s .c o m public void indexDocsByPath(Path path, String repoName, String repoLocations, String repoRemoteLocation, boolean existingRepo) { SearchcodeLib scl = Singleton.getSearchCodeLib(); // Should have data object by this point List<String> fileLocations = new ArrayList<>(); Queue<CodeIndexDocument> codeIndexDocumentQueue = Singleton.getCodeIndexQueue(); // Convert once outside the main loop String fileRepoLocations = FilenameUtils.separatorsToUnix(repoLocations); boolean lowMemory = this.LOWMEMORY; try { Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { while (CodeIndexer.shouldPauseAdding()) { Singleton.getLogger().info("Pausing parser."); try { Thread.sleep(SLEEPTIME); } catch (InterruptedException ex) { } } // Convert Path file to unix style that way everything is easier to reason about String fileParent = FilenameUtils.separatorsToUnix(file.getParent().toString()); String fileToString = FilenameUtils.separatorsToUnix(file.toString()); String fileName = file.getFileName().toString(); String md5Hash = Values.EMPTYSTRING; if (fileParent.endsWith("/.svn") || fileParent.contains("/.svn/")) { return FileVisitResult.CONTINUE; } List<String> codeLines; try { codeLines = Helpers.readFileLines(fileToString, MAXFILELINEDEPTH); } catch (IOException ex) { return FileVisitResult.CONTINUE; } try { FileInputStream fis = new FileInputStream(new File(fileToString)); md5Hash = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis); fis.close(); } catch (IOException ex) { Singleton.getLogger().warning("Unable to generate MD5 for " + fileToString); } // is the file minified? if (scl.isMinified(codeLines)) { Singleton.getLogger().info("Appears to be minified will not index " + fileToString); return FileVisitResult.CONTINUE; } String languageName = scl.languageGuesser(fileName, codeLines); String fileLocation = fileToString.replace(fileRepoLocations, Values.EMPTYSTRING) .replace(fileName, Values.EMPTYSTRING); String fileLocationFilename = fileToString.replace(fileRepoLocations, Values.EMPTYSTRING); String repoLocationRepoNameLocationFilename = fileToString; String newString = getBlameFilePath(fileLocationFilename); String codeOwner = getInfoExternal(codeLines.size(), repoName, fileRepoLocations, newString) .getName(); // If low memory don't add to the queue, just index it directly if (lowMemory) { CodeIndexer.indexDocument(new CodeIndexDocument(repoLocationRepoNameLocationFilename, repoName, fileName, fileLocation, fileLocationFilename, md5Hash, languageName, codeLines.size(), StringUtils.join(codeLines, " "), repoRemoteLocation, codeOwner)); } else { Singleton.incrementCodeIndexLinesCount(codeLines.size()); codeIndexDocumentQueue.add(new CodeIndexDocument(repoLocationRepoNameLocationFilename, repoName, fileName, fileLocation, fileLocationFilename, md5Hash, languageName, codeLines.size(), StringUtils.join(codeLines, " "), repoRemoteLocation, codeOwner)); } fileLocations.add(fileLocationFilename); return FileVisitResult.CONTINUE; } }); } catch (IOException ex) { Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + "\n with message: " + ex.getMessage()); } if (existingRepo) { CodeSearcher cs = new CodeSearcher(); List<String> indexLocations = cs.getRepoDocuments(repoName); for (String file : indexLocations) { if (!fileLocations.contains(file)) { Singleton.getLogger().info("Missing from disk, removing from index " + file); try { CodeIndexer.deleteByFileLocationFilename(file); } catch (IOException ex) { Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + "\n with message: " + ex.getMessage()); } } } } }
From source file:org.bonitasoft.platform.configuration.impl.ConfigurationServiceImpl.java
@Override public void storeLicenses(File licensesFolder) throws PlatformException { final Path path = licensesFolder.toPath(); List<BonitaConfiguration> bonitaConfigurations = new ArrayList<>(); LicensesResourceVisitor licensesResourceVisitor = new LicensesResourceVisitor(bonitaConfigurations); try {//from w w w.j a v a 2 s . c o m Files.walkFileTree(path, licensesResourceVisitor); cleanAndStoreConfiguration(bonitaConfigurations, LICENSES, NON_TENANT_RESOURCE); } catch (IOException e) { throw new PlatformException(e); } }
From source file:org.apache.openaz.xacml.pdp.test.TestBase.java
/** * Removes all the Response* files from the results directory. *//* w w w . ja va 2 s . co m*/ public void removeResults() { try { // // Determine where the results are supposed to be written to // Path resultsPath; if (this.output != null) { resultsPath = this.output; } else { resultsPath = Paths.get(this.directory.toString(), "results"); } // // Walk the files // Files.walkFileTree(resultsPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (file.getFileName().toString().startsWith("Response")) { Files.delete(file); } return super.visitFile(file, attrs); } }); } catch (IOException e) { logger.error("Failed to removeRequests from " + this.directory + " " + e); } }
From source file:com.searchcode.app.jobs.IndexGitRepoJob.java
/** * Indexes all the documents in the path provided. Will also remove anything from the index if not on disk * Generally this is a slow update used only for the inital clone of a repository * NB this can be used for updates but it will be much slower as it needs to to walk the contents of the disk *//* ww w . j a va 2s. c o m*/ public void indexDocsByPath(Path path, String repoName, String repoLocations, String repoRemoteLocation, boolean existingRepo) { SearchcodeLib scl = Singleton.getSearchCodeLib(); // Should have data object by this point List<String> fileLocations = new ArrayList<>(); Queue<CodeIndexDocument> codeIndexDocumentQueue = Singleton.getCodeIndexQueue(); // Convert once outside the main loop String fileRepoLocations = FilenameUtils.separatorsToUnix(repoLocations); boolean lowMemory = this.LOWMEMORY; boolean useSystemGit = this.USESYSTEMGIT; try { Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { while (CodeIndexer.shouldPauseAdding()) { Singleton.getLogger().info("Pausing parser."); try { Thread.sleep(SLEEPTIME); } catch (InterruptedException ex) { } } // Convert Path file to unix style that way everything is easier to reason about String fileParent = FilenameUtils.separatorsToUnix(file.getParent().toString()); String fileToString = FilenameUtils.separatorsToUnix(file.toString()); String fileName = file.getFileName().toString(); String md5Hash = Values.EMPTYSTRING; if (fileParent.endsWith("/.git") || fileParent.contains("/.git/")) { return FileVisitResult.CONTINUE; } List<String> codeLines; try { codeLines = Helpers.readFileLines(fileToString, MAXFILELINEDEPTH); } catch (IOException ex) { return FileVisitResult.CONTINUE; } try { FileInputStream fis = new FileInputStream(new File(fileToString)); md5Hash = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis); fis.close(); } catch (IOException ex) { Singleton.getLogger().warning("Unable to generate MD5 for " + fileToString); } // is the file minified? if (scl.isMinified(codeLines)) { Singleton.getLogger().info("Appears to be minified will not index " + fileToString); return FileVisitResult.CONTINUE; } String languageName = scl.languageGuesser(fileName, codeLines); String fileLocation = fileToString.replace(fileRepoLocations, Values.EMPTYSTRING) .replace(fileName, Values.EMPTYSTRING); String fileLocationFilename = fileToString.replace(fileRepoLocations, Values.EMPTYSTRING); String repoLocationRepoNameLocationFilename = fileToString; String newString = getBlameFilePath(fileLocationFilename); List<CodeOwner> owners; if (useSystemGit) { owners = getBlameInfoExternal(codeLines.size(), repoName, fileRepoLocations, newString); } else { owners = getBlameInfo(codeLines.size(), repoName, fileRepoLocations, newString); } String codeOwner = scl.codeOwner(owners); // If low memory don't add to the queue, just index it directly if (lowMemory) { CodeIndexer.indexDocument(new CodeIndexDocument(repoLocationRepoNameLocationFilename, repoName, fileName, fileLocation, fileLocationFilename, md5Hash, languageName, codeLines.size(), StringUtils.join(codeLines, " "), repoRemoteLocation, codeOwner)); } else { Singleton.incrementCodeIndexLinesCount(codeLines.size()); codeIndexDocumentQueue.add(new CodeIndexDocument(repoLocationRepoNameLocationFilename, repoName, fileName, fileLocation, fileLocationFilename, md5Hash, languageName, codeLines.size(), StringUtils.join(codeLines, " "), repoRemoteLocation, codeOwner)); } fileLocations.add(fileLocationFilename); return FileVisitResult.CONTINUE; } }); } catch (IOException ex) { Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + "\n with message: " + ex.getMessage()); } if (existingRepo) { CodeSearcher cs = new CodeSearcher(); List<String> indexLocations = cs.getRepoDocuments(repoName); for (String file : indexLocations) { if (!fileLocations.contains(file)) { Singleton.getLogger().info("Missing from disk, removing from index " + file); try { CodeIndexer.deleteByFileLocationFilename(file); } catch (IOException ex) { Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + "\n with message: " + ex.getMessage()); } } } } }
From source file:org.roda.core.common.RodaUtils.java
/** * @deprecated 20160907 hsilva: not seeing any method using it, so it will be * removed soon/*from www. j a va 2 s. c om*/ */ @Deprecated public static long getPathSize(Path startPath) throws IOException { final AtomicLong size = new AtomicLong(0); Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { size.addAndGet(attrs.size()); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { // Skip folders that can't be traversed return FileVisitResult.CONTINUE; } }); return size.get(); }
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 .j a va 2 s . c o 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:fr.pilato.elasticsearch.crawler.fs.framework.FsCrawlerUtil.java
/** * Unzip a jar file/* w w w. ja v a2s. c o m*/ * @param jarFile Jar file url like file:/path/to/foo.jar * @param destination Directory where we want to extract the content to * @throws IOException In case of any IO problem */ public static void unzip(String jarFile, Path destination) throws IOException { Map<String, String> zipProperties = new HashMap<>(); /* We want to read an existing ZIP File, so we set this to false */ zipProperties.put("create", "false"); zipProperties.put("encoding", "UTF-8"); URI zipFile = URI.create("jar:" + jarFile); try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, zipProperties)) { Path rootPath = zipfs.getPath("/"); Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { Path targetPath = destination.resolve(rootPath.relativize(dir).toString()); if (!Files.exists(targetPath)) { Files.createDirectory(targetPath); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.copy(file, destination.resolve(rootPath.relativize(file).toString()), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING); return FileVisitResult.CONTINUE; } }); } }
From source file:org.sejda.core.service.TaskTestContext.java
@Override public void close() throws IOException { IOUtils.closeQuietly(streamOutput);/*from w w w . j a v a2 s . c o m*/ this.streamOutput = null; IOUtils.closeQuietly(outputDocument); this.outputDocument = null; if (nonNull(fileOutput)) { if (fileOutput.isDirectory()) { Files.walkFileTree(fileOutput.toPath(), 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; } }); } Files.deleteIfExists(fileOutput.toPath()); } this.fileOutput = null; }
From source file:io.werval.cli.DamnSmallDevShell.java
private static void cleanCommand(boolean debug, File tmpDir) { try {//from w w w .j a v a 2s.c o m // Clean if (tmpDir.exists()) { Files.walkFileTree(tmpDir.toPath(), new DeltreeFileVisitor()); } // Inform user System.out.println( "Temporary files " + (debug ? "in '" + tmpDir.getAbsolutePath() + "' " : "") + "deleted."); } catch (IOException ex) { ex.printStackTrace(System.err); } }
From source file:com.rover12421.shaka.apktool.lib.AndrolibAj.java
/** * @param joinPoint//from w w w .j a v a2s.c o m * @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(); } }