List of usage examples for java.nio.file SimpleFileVisitor SimpleFileVisitor
protected SimpleFileVisitor()
From source file:com.cloudbees.clickstack.util.Files2.java
public static void unzip(@Nonnull Path zipFile, @Nonnull final Path destDir) throws RuntimeIOException { try {/*from ww w . ja va2s. c om*/ //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/*from ww w . j av a 2s . c om*/ * @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 va2s.c om*/ 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:com.facebook.buck.io.filesystem.impl.DefaultProjectFilesystemTest.java
@Test public void testWalkFileTreeWhenProjectRootIsWorkingDir() throws IOException { ProjectFilesystem projectFilesystem = TestProjectFilesystems .createProjectFilesystem(Paths.get(".").toAbsolutePath()); ImmutableList.Builder<String> fileNames = ImmutableList.builder(); Path pathRelativeToProjectRoot = Paths .get("test/com/facebook/buck/io/testdata/directory_traversal_ignore_paths"); projectFilesystem.walkRelativeFileTree(pathRelativeToProjectRoot, new SimpleFileVisitor<Path>() { @Override/*from w ww . java2 s. com*/ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { fileNames.add(file.getFileName().toString()); return FileVisitResult.CONTINUE; } }); assertThat(fileNames.build(), containsInAnyOrder("file", "a_file", "b_file", "b_c_file", "b_d_file")); }
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;//ww w. j av a 2 s. c o m } 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.rover12421.shaka.apktool.lib.AndrolibAj.java
/** * @param joinPoint/*from w w w . ja v a2 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: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 ww .j a v a 2s .c o m 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: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/*www .j av a 2 s .c o m*/ 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 ww w . ja v a2s .com @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:org.fao.geonet.api.records.MetadataInsertDeleteApi.java
@ApiOperation(value = "Add a record", notes = "Add one or more record from an XML fragment, " + "URL or file in a folder on the catalog server. When loading" + "from the catalog server folder, it might be faster to use a " + "local filesystem harvester.", nickname = "insert") @RequestMapping(method = { RequestMethod.PUT }, produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE }) @ApiResponses(value = { @ApiResponse(code = 201, message = API_PARAM_REPORT_ABOUT_IMPORTED_RECORDS), @ApiResponse(code = 403, message = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_EDITOR) }) @PreAuthorize("hasRole('Editor')") @ResponseStatus(HttpStatus.CREATED)// w w w . ja va 2 s. c o m public @ResponseBody SimpleMetadataProcessingReport insert( @ApiParam(value = API_PARAM_RECORD_TYPE, required = false, defaultValue = "METADATA") @RequestParam(required = false, defaultValue = "METADATA") final MetadataType metadataType, @ApiParam(value = "XML fragment.", required = false) @RequestBody(required = false) String xml, @ApiParam(value = "URL of a file to download and insert.", required = false) @RequestParam(required = false) String[] url, @ApiParam(value = "Server folder where to look for files.", required = false) @RequestParam(required = false) String serverFolder, @ApiParam(value = "(Server folder import only) Recursive search in folder.", required = false) @RequestParam(required = false, defaultValue = "false") final boolean recursiveSearch, @ApiParam(value = "(MEF file only) Assign to current catalog.", required = false) @RequestParam(required = false, defaultValue = "false") final boolean assignToCatalog, @ApiParam(value = API_PARAM_RECORD_UUID_PROCESSING, required = false, defaultValue = "NOTHING") @RequestParam(required = false, defaultValue = "NOTHING") final MEFLib.UuidAction uuidProcessing, @ApiParam(value = API_PARAP_RECORD_GROUP, required = false) @RequestParam(required = false) final String group, @ApiParam(value = API_PARAM_RECORD_TAGS, required = false) @RequestParam(required = false) final String[] category, @ApiParam(value = API_PARAM_RECORD_VALIDATE, required = false) @RequestParam(required = false, defaultValue = "false") final boolean rejectIfInvalid, @ApiParam(value = API_PARAM_RECORD_XSL, required = false, defaultValue = "_none_") @RequestParam(required = false, defaultValue = "_none_") final String transformWith, @ApiParam(value = API_PARAM_FORCE_SCHEMA, required = false) @RequestParam(required = false) String schema, @ApiParam(value = "(experimental) Add extra information to the record.", required = false) @RequestParam(required = false) final String extra, HttpServletRequest request) throws Exception { if (url == null && xml == null && serverFolder == null) { throw new IllegalArgumentException( String.format("XML fragment or a URL or a server folder MUST be provided.")); } SimpleMetadataProcessingReport report = new SimpleMetadataProcessingReport(); ApplicationContext applicationContext = ApplicationContextHolder.get(); if (xml != null) { Element element = null; try { element = Xml.loadString(xml, false); } catch (JDOMParseException ex) { throw new IllegalArgumentException( String.format("XML fragment is invalid. Error is %s", ex.getMessage())); } Pair<Integer, String> pair = loadRecord(metadataType, Xml.loadString(xml, false), uuidProcessing, group, category, rejectIfInvalid, false, transformWith, schema, extra, request); report.addMetadataInfos(pair.one(), String.format("Metadata imported from XML with UUID '%s'", pair.two())); triggerImportEvent(request, pair.two()); report.incrementProcessedRecords(); } if (url != null) { for (String u : url) { Element xmlContent = null; try { xmlContent = Xml.loadFile(ApiUtils.downloadUrlInTemp(u)); } catch (Exception e) { report.addError(e); } if (xmlContent != null) { Pair<Integer, String> pair = loadRecord(metadataType, xmlContent, uuidProcessing, group, category, rejectIfInvalid, false, transformWith, schema, extra, request); report.addMetadataInfos(pair.one(), String.format("Metadata imported from URL with UUID '%s'", pair.two())); triggerImportEvent(request, pair.two()); } report.incrementProcessedRecords(); } } if (serverFolder != null) { Path serverFolderPath = IO.toPath(serverFolder); final List<Path> files = Lists.newArrayList(); final MEFLib.MefOrXmlFileFilter predicate = new MEFLib.MefOrXmlFileFilter(); if (recursiveSearch) { Files.walkFileTree(serverFolderPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (predicate.accept(file)) { files.add(file); } return FileVisitResult.CONTINUE; } }); } else { try (DirectoryStream<Path> paths = Files.newDirectoryStream(serverFolderPath, predicate)) { for (Path file : paths) { files.add(file); } } } if (files.size() == 0) { throw new Exception( String.format("No XML or MEF or ZIP file found in server folder '%s'.", serverFolder)); } SettingManager settingManager = ApplicationContextHolder.get().getBean(SettingManager.class); ServiceContext context = ApiUtils.createServiceContext(request); for (Path f : files) { if (MEFLib.isValidArchiveExtensionForMEF(f.getFileName().toString())) { try { MEFLib.Version version = MEFLib.getMEFVersion(f); List<String> ids = MEFLib.doImport(version == MEFLib.Version.V1 ? "mef" : "mef2", uuidProcessing, transformWith, settingManager.getSiteId(), metadataType, category, group, rejectIfInvalid, assignToCatalog, context, f); for (String id : ids) { report.addMetadataInfos(Integer.parseInt(id), String.format("Metadata imported from MEF with id '%s'", id)); triggerCreationEvent(request, id); report.incrementProcessedRecords(); } } catch (Exception e) { report.addError(e); report.addInfos(String.format("Failed to import MEF file '%s'. Check error for details.", f.getFileName().toString())); } } else { try { Pair<Integer, String> pair = loadRecord(metadataType, Xml.loadFile(f), uuidProcessing, group, category, rejectIfInvalid, false, transformWith, schema, extra, request); report.addMetadataInfos(pair.one(), String.format("Metadata imported from server folder with UUID '%s'", pair.two())); triggerCreationEvent(request, pair.two()); } catch (Exception e) { report.addError(e); } report.incrementProcessedRecords(); } } } report.close(); return report; }