List of usage examples for java.nio.file Path relativize
Path relativize(Path other);
From source file:org.dataconservancy.dcs.util.FilePathUtil.java
/** * This method has been deprecated use the Java Path relativize method instead. * Strips the base directory out of a file path to make a relative file path. If the file doesn't contain the base path the original path is returned. * @param basePath The base directory path to remove from the file path. * @param toRelativize The file to be made relative. * @return The relative file path created by removing the base directory. Returns null if the file to relative is null. *//*from w w w.j a va 2s . co m*/ @Deprecated public static String relativizePath(String basePath, File toRelativize) { if (toRelativize == null) { return null; } if (basePath == null || !toRelativize.getPath().startsWith(basePath)) { return toRelativize.getPath(); } Path path = FileSystems.getDefault().getPath(basePath); Path relativePath = path.relativize(toRelativize.toPath()); return relativePath.toString(); }
From source file:org.sherlok.FileBased.java
/** * Compute the relative path from a give absolute path and the resource * directory./*from ww w. j ava 2s .c o m*/ */ public static String getRelativePathToResources(File absolutePath) { checkArgument(absolutePath.isAbsolute()); Path absolute = absolutePath.toPath(); Path base = new File(RUTA_RESOURCES_PATH).getAbsoluteFile().toPath(); return base.relativize(absolute).toString(); }
From source file:com.puppycrawl.tools.checkstyle.utils.CommonUtils.java
/** * Constructs a normalized relative path between base directory and a given path. * * @param baseDirectory//ww w. jav a 2 s . c om * the base path to which given path is relativized * @param path * the path to relativize against base directory * @return the relative normalized path between base directory and * path or path if base directory is null. */ public static String relativizeAndNormalizePath(final String baseDirectory, final String path) { if (baseDirectory == null) { return path; } final Path pathAbsolute = Paths.get(path).normalize(); final Path pathBase = Paths.get(baseDirectory).normalize(); return pathBase.relativize(pathAbsolute).toString(); }
From source file:org.apache.geode.management.internal.configuration.utils.ZipUtils.java
public static void zipDirectory(Path sourceDirectory, Path targetFile) throws IOException { Path p = Files.createFile(targetFile); try (ZipOutputStream zs = new ZipOutputStream(Files.newOutputStream(p))) { Files.walk(sourceDirectory).filter(path -> !Files.isDirectory(path)).forEach(path -> { ZipEntry zipEntry = new ZipEntry(sourceDirectory.relativize(path).toString()); try { zs.putNextEntry(zipEntry); zs.write(Files.readAllBytes(path)); zs.closeEntry();/* w ww . jav a2s . c om*/ } catch (Exception e) { throw new RuntimeException("Unable to write zip file", e); } }); } }
From source file:org.ng200.openolympus.FileAccess.java
public static void copyDirectory(final Path from, final Path to, CopyOption... copyOptions) throws IOException { try (Stream<Path> files = Files.walk(from)) { for (final Path file : files.collect(Collectors.toList())) { final Path target = to.resolve(from.relativize(file)); Files.createDirectories(target.getParent()); Files.copy(file, target, copyOptions); }/*w w w. j a v a 2 s . c om*/ } }
From source file:org.phenotips.variantstore.shared.ResourceManager.java
/** * Copy resources recursively from a path on the filesystem to the destination folder. * * @param source//w w w.j a v a2 s . co m * @param dest * @throws IOException */ private static void copyResourcesFromFilesystem(final Path source, final Path dest) throws IOException { Files.walkFileTree(source, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Path relative = source.relativize(file); Files.copy(file, dest.resolve(relative)); return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { Path relative = source.relativize(dir); Files.createDirectory(dest.resolve(relative)); return FileVisitResult.CONTINUE; } }); }
From source file:org.eclipse.winery.yaml.common.Utils.java
public static InputStream zipPath(Path path) { File zipFile = new File(path.getParent().toString() + File.separator + "tmp.zip"); try (FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos)) { Files.walk(path).filter(Files::isRegularFile).forEach(file -> { try { zos.putNextEntry(new ZipEntry(path.relativize(file).toString())); Files.copy(file, zos); zos.closeEntry();//from ww w. ja v a 2s .com } catch (Exception e) { } }); return new FileInputStream(zipFile); } catch (Exception e) { logger.error("Create zip tmp file error: ", e); } return null; }
From source file:org.roda.core.common.monitor.TransferredResourcesScanner.java
public static TransferredResource instantiateTransferredResource(Path resourcePath, Path basePath) { Path relativeToBase = basePath.relativize(resourcePath); TransferredResource tr = new TransferredResource(); tr.setFile(!FSUtils.isDirectory(resourcePath)); tr.setFullPath(resourcePath.toString()); String id = relativeToBase.toString(); tr.setId(id);//from w w w. j av a2s. co m tr.setUUID(IdUtils.getTransferredResourceUUID(relativeToBase)); tr.setName(resourcePath.getFileName().toString()); tr.setRelativePath(relativeToBase.toString()); if (relativeToBase.getParent() != null) { String parentId = relativeToBase.getParent().toString(); tr.setParentId(parentId); tr.setParentUUID(IdUtils.createUUID(parentId)); } List<String> ancestors = new ArrayList<>(); StringBuilder temp = new StringBuilder(); Iterator<Path> pathIterator = relativeToBase.iterator(); while (pathIterator.hasNext()) { temp.append(pathIterator.next().toString()); ancestors.add(temp.toString()); temp.append("/"); } ancestors.remove(ancestors.size() - 1); tr.setAncestorsPaths(ancestors); return tr; }
From source file:io.fabric8.profiles.ProfilesHelpers.java
public static void recusivelyCollectFileListing(ArrayList<String> rc, Path base, Path directory) throws IOException { try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) { for (Path path : directoryStream) { if (Files.isDirectory(path)) { recusivelyCollectFileListing(rc, base, path); } else { rc.add(base.relativize(path).toString()); }//from w w w . j a va2s . c o m } } }
From source file:org.apache.geode.management.internal.cli.util.MergeLogs.java
static File mergeLogFile(String dirName) throws Exception { Path dir = Paths.get(dirName); List<File> logsToMerge = findLogFilesToMerge(dir.toFile()); InputStream[] logFiles = new FileInputStream[logsToMerge.size()]; String[] logFileNames = new String[logFiles.length]; for (int i = 0; i < logsToMerge.size(); i++) { try {// ww w . j av a2 s . c om logFiles[i] = new FileInputStream(logsToMerge.get(i)); logFileNames[i] = dir.relativize(logsToMerge.get(i).toPath()).toString(); } catch (FileNotFoundException e) { throw new Exception(logsToMerge.get(i) + " " + CliStrings.EXPORT_LOGS__MSG__FILE_DOES_NOT_EXIST); } } PrintWriter mergedLog = null; File mergedLogFile = null; try { String mergeLog = dirName + File.separator + "merge_" + new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new java.util.Date()) + ".log"; mergedLogFile = new File(mergeLog); mergedLog = new PrintWriter(mergedLogFile); boolean flag = MergeLogFiles.mergeLogFiles(logFiles, logFileNames, mergedLog); } catch (FileNotFoundException e) { throw new Exception("FileNotFoundException in creating PrintWriter in MergeLogFiles" + e.getMessage()); } catch (Exception e) { throw new Exception("Exception in creating PrintWriter in MergeLogFiles" + e.getMessage()); } return mergedLogFile; }