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:org.apache.taverna.databundle.DataBundles.java

private static Path anyExtension(Path path) throws IOException {
    return anyExtension(path.getParent(), path.getFileName().toString());
}

From source file:objective.taskboard.utils.ZipUtils.java

public static void zip(Path input, Path output) {
    try (Stream<Path> stream = walk(input)) {
        File outputParent = output.toFile().getParentFile();
        if (outputParent != null && !outputParent.exists())
            createDirectories(outputParent.toPath());
        try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(output))) {
            stream.filter(path -> path.toFile().isFile()).forEach(path -> {
                try {
                    Path inputDirectory = input.toFile().isDirectory() ? input : input.getParent();
                    ZipEntry entry = new ZipEntry(inputDirectory.relativize(path).toString());
                    zipOutputStream.putNextEntry(entry);
                    copy(path, zipOutputStream);
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }//from   w w  w .j  av  a2 s  .co  m
            });
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.mitre.mpf.wfm.util.PropertiesUtil.java

public static void createParentDir(Resource resource) throws IOException {
    Path resourcePath = Paths.get(resource.getURI());
    Path resourceDir = resourcePath.getParent();
    if (Files.notExists(resourceDir)) {
        log.info("Directory {} doesn't exist. Creating it now.", resourceDir);
        Files.createDirectories(resourceDir);
    }//from   w w  w. j  a  v a2  s . c  om
}

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

public static void addFileToZip(@Nonnull Path sourceStream, @Nonnull Path destZipFile, @Nonnull String destFile,
        CopyOption... copyOptions) {
    try (FileSystem zipFileSystem = createZipFileSystem(destZipFile, false)) {
        final Path root = zipFileSystem.getPath("/");
        Path destFilePath = root.resolve(destFile);
        Files.createDirectories(destFilePath.getParent());
        logger.debug("Copy {} to {}, destFile: {}", sourceStream, destFile, destFilePath);

        Files.copy(sourceStream, destFilePath, copyOptions);
    } catch (IOException e) {
        throw new RuntimeIOException("Exception adding file " + sourceStream + " to " + destZipFile, e);
    }//from  w ww.  j a  va 2 s. c o m
}

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

public static void addFileToZip(@Nonnull InputStream sourceFile, @Nonnull Path destZipFile,
        @Nonnull String destFile, CopyOption... copyOptions) {
    try (FileSystem zipFileSystem = createZipFileSystem(destZipFile, false)) {
        final Path root = zipFileSystem.getPath("/");
        Path destFilePath = root.resolve(destFile);
        Files.createDirectories(destFilePath.getParent());
        logger.debug("Copy {} to {}, destFile: {}", sourceFile, destFile, destFilePath);

        Files.copy(sourceFile, destFilePath, copyOptions);
    } catch (IOException e) {
        throw new RuntimeIOException("Exception adding file " + sourceFile + " to " + destZipFile, e);
    }//  w  ww  .j av  a2 s .c  om
}

From source file:org.apache.taverna.databundle.DataBundles.java

public static void deleteAllExtensions(final Path file) throws IOException {
    Filter<Path> filter = new ExtensionIgnoringFilter(file);
    try (DirectoryStream<Path> ds = newDirectoryStream(file.getParent(), filter)) {
        for (Path p : ds)
            deleteRecursively(p);//w  w w. j a v a  2s .c om
    }
}

From source file:nl.coinsweb.sdk.FileManager.java

/**
 * Extracts all the content of the .ccr-file specified in the constructor to [TEMP_ZIP_PATH] / [internalRef].
 *
 *//* w w w.j a v a  2 s .  c o  m*/
public static void unzipTo(File sourceFile, Path destinationPath) {

    byte[] buffer = new byte[1024];
    String startFolder = null;

    try {

        // Get the zip file content
        ZipInputStream zis = new ZipInputStream(new FileInputStream(sourceFile));
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            if (ze.isDirectory()) {
                ze = zis.getNextEntry();
                continue;
            }

            String fileName = ze.getName();

            log.info("Dealing with file " + fileName);

            // If the first folder is a somename/bim/file.ref skip it
            Path filePath = Paths.get(fileName);
            Path pathPath = filePath.getParent();

            if (pathPath.endsWith("bim") || pathPath.endsWith("bim/repository") || pathPath.endsWith("doc")
                    || pathPath.endsWith("woa")) {

                Path pathRoot = pathPath.endsWith("repository") ? pathPath.getParent().getParent()
                        : pathPath.getParent();

                String prefix = "";
                if (pathRoot != null) {
                    prefix = pathRoot.toString();
                }

                if (startFolder == null) {
                    startFolder = prefix;
                    log.debug("File root set to: " + startFolder);

                } else if (startFolder != null && !prefix.equals(startFolder)) {
                    throw new InvalidContainerFileException(
                            "The container file has an inconsistent file root, was " + startFolder
                                    + ", now dealing with " + prefix + ".");
                }
            } else {
                log.debug("Skipping file: " + filePath.toString());
                continue;
            }

            String insideStartFolder = filePath.toString().substring(startFolder.length());
            File newFile = new File(destinationPath + "/" + insideStartFolder);
            log.info("Extract " + newFile);

            // Create all non exists folders
            // else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.close();
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:org.apdplat.superword.tools.PdfParser.java

/**
 * PDF???//from w w w  . jav  a 2  s.c  o m
 * @param file
 * @return
 */
private static String prepareTarget(Path file) {
    try {
        String fileName = file.toFile().getAbsolutePath();
        String targetName = "src/main/resources/it" + fileName
                .replace(file.getParent().getParent().toFile().getAbsolutePath(), "").replace(".pdf", "")
                + ".txt";
        Path target = Paths.get(targetName);
        //??
        Files.deleteIfExists(target);
        //
        if (Files.notExists(target.getParent())) {
            Files.createDirectories(target.getParent());
        }
        return targetName;
    } catch (Exception e) {
        e.printStackTrace();
    }
    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);// ww w . j  a  v a2 s  .  c o  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:org.apache.taverna.databundle.DataBundles.java

public static Path setError(Path errorPath, String message, String trace, Path... causedBy) throws IOException {
    errorPath = withExtension(errorPath, DOT_ERR);
    // Silly \n-based format
    List<String> errorDoc = new ArrayList<>();
    for (Path cause : causedBy) {
        Path relCause = errorPath.getParent().relativize(cause);
        errorDoc.add(relCause.toString());
    }/*w ww. j  a  v a  2 s  .  com*/
    errorDoc.add(""); // Our magic separator
    errorDoc.add(message);
    errorDoc.add(trace);
    checkExistingAnyExtension(errorPath);
    write(errorPath, errorDoc, UTF8, TRUNCATE_EXISTING, CREATE);
    return errorPath;
}