Example usage for java.nio.file Path getParent

List of usage examples for java.nio.file Path getParent

Introduction

In this page you can find the example usage for java.nio.file Path getParent.

Prototype

Path getParent();

Source Link

Document

Returns the parent path, or null if this path does not have a parent.

Usage

From source file:fr.duminy.jbackup.core.TestUtils.java

public static Path createFile(Path file, String content) throws IOException {
    if (file.getParent() != null) {
        Files.createDirectories(file.getParent());
    }//from   ww  w. j av  a 2s.c o  m

    Files.write(file, content.getBytes());
    return file;
}

From source file:com.blackducksoftware.integration.hub.detect.workflow.codelocation.FileNameUtils.java

public static String relativize(final String from, final String to) {
    String relative = to;/* www .j av  a  2  s. co  m*/
    try {
        final Path toPath = new File(to).toPath();
        final Path fromPath = new File(from).toPath();
        final Path fromParentPath = fromPath.getParent();
        final Path relativePath = fromParentPath.relativize(toPath);
        final List<String> relativePieces = new ArrayList<>();
        for (int i = 0; i < relativePath.getNameCount(); i++) {
            relativePieces.add(relativePath.getName(i).toFile().getName());
        }
        relative = StringUtils.join(relativePieces, "/");
    } catch (final Exception e) {
        logger.info(String.format("Unable to relativize path, full source path will be used: %s", to));
        logger.debug("The reason relativize failed: ", e);
    }

    return relative;
}

From source file:org.metaservice.core.file.FileUriUtils.java

public static FileIdentifier storeFile(InputStream inputStream) throws FileProcessingException {

    try {/*ww w .j  a  v a 2  s .  c om*/
        File localDir = com.google.common.io.Files.createTempDir();
        File localFile = new File(localDir.toString() + "/x");
        IOUtils.copy(inputStream, new FileOutputStream(localFile));
        HashCode x = com.google.common.io.Files.hash(localFile, Hashing.sha1());
        long size = localFile.length();
        Path target = getFile(x.toString(), size);
        Files.createDirectories(target.getParent());
        Files.move(localFile.toPath(), target);
        return new FileIdentifier(x.toString(), size);
    } catch (IOException e) {
        throw new FileProcessingException(e);
    }
}

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();/*w w w . ja  va 2s  . c o  m*/
            } catch (Exception e) {

            }
        });
        return new FileInputStream(zipFile);
    } catch (Exception e) {
        logger.error("Create zip tmp file error: ", e);
    }
    return null;
}

From source file:azkaban.execapp.AzkabanExecutorServerTest.java

private static String getSqlScriptsDir() throws IOException {
    // Dummy because any resource file works.
    URL resource = AzkabanExecutorServerTest.class.getClassLoader().getResource("test.file");
    final String dummyResourcePath = requireNonNull(resource).getPath();
    Path resources = Paths.get(dummyResourcePath).getParent();
    Path azkabanRoot = resources.getParent().getParent().getParent().getParent();

    File sqlScriptDir = Paths.get(azkabanRoot.toString(), AZKABAN_DB_SQL_PATH).toFile();
    return props.getString(AzkabanDatabaseSetup.DATABASE_SQL_SCRIPT_DIR, sqlScriptDir.getCanonicalPath());
}

From source file:com.ttech.cordovabuild.infrastructure.archive.ArchiveUtils.java

private static void extractArchive(Path localPath, ArchiveInputStream ais) throws IOException {
    ArchiveEntry ae;//from w w w . jav  a  2 s  .co m
    while ((ae = ais.getNextEntry()) != null) {
        if (ae.isDirectory()) {
            continue;
        }
        Path filePath = localPath.resolve(ae.getName());
        if (!filePath.getParent().equals(localPath))
            Files.createDirectories(filePath.getParent());
        try (OutputStream outputStream = Files.newOutputStream(filePath)) {
            IOUtils.copy(ais, outputStream);
        }
    }
}

From source file:org.jetbrains.webdemo.backend.executor.ExecutorUtils.java

private static Path storeFilesInTemporaryDirectory(Map<String, byte[]> files) throws IOException {
    Path outputDir = Paths.get(BackendSettings.OUTPUT_DIRECTORY, "tmp", String.valueOf(new Random().nextInt()));
    for (String fileName : files.keySet()) {
        Path filePath = outputDir.resolve(fileName);
        filePath.getParent().toFile().mkdirs();
        Files.write(filePath, files.get(fileName));
    }/*from w w  w.jav  a  2 s  .  c  om*/
    return outputDir;
}

From source file:azkaban.soloserver.AzkabanSingleServerTest.java

private static String getSqlScriptsDir() throws IOException {
    // Dummy because any resource file works.
    Path resources = Paths.get(getConfPath()).getParent();
    Path azkabanRoot = resources.getParent().getParent().getParent().getParent();

    File sqlScriptDir = Paths.get(azkabanRoot.toString(), AZKABAN_DB_SQL_PATH).toFile();
    return sqlScriptDir.getCanonicalPath();
}

From source file:io.github.alechenninger.monarch.Main.java

private static void ensureParentDirectories(Path path) throws IOException {
    Path parent = path.getParent();
    if (parent != null) {
        Files.createDirectories(parent);
    }//from   w  w  w  .  j a v a  2  s.  c om
}

From source file:demo.utils.MiniClusterUtils.java

public static void startMiniCluster() {
    if (clusterLauncher != null) {
        throw new IllegalStateException("MiniClustrer is currently running");
    }//  w  w w  . j  av  a2 s  .  com
    File file = new File(System.getProperty("user.dir"));
    Path path = Paths.get(file.getAbsolutePath());
    Path parentPath = path.getParent();
    String[] resources = file.list();
    for (String resource : resources) {
        if (resource.equals("yaya-demo")) {
            parentPath = path;
            break;
        }
    }
    File miniClusterExe = new File(
            parentPath.toString() + "/yarn-test-cluster/build/install/yarn-test-cluster/bin/yarn-test-cluster");
    System.out.println(miniClusterExe.getAbsolutePath());
    if (!miniClusterExe.exists()) {
        logger.info("BUILDING MINI_CLUSTER");
        CommandProcessLauncher buildLauncher = new CommandProcessLauncher(
                path.toString() + "/build-mini-cluster");
        buildLauncher.launch();
    }
    Assert.isTrue(miniClusterExe.exists(), "Failed to find mini-cluster executable");
    clusterLauncher = new CommandProcessLauncher(miniClusterExe.getAbsolutePath());
    executor = Executors.newSingleThreadExecutor();
    executor.execute(new Runnable() {
        @Override
        public void run() {
            logger.info("STARTING MINI_CLUSTER");
            clusterLauncher.launch();
        }
    });
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}