Example usage for java.nio.file Path relativize

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

Introduction

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

Prototype

Path relativize(Path other);

Source Link

Document

Constructs a relative path between this path and a given path.

Usage

From source file:org.wso2.identity.integration.common.extension.server.IdentityServerExtension.java

private void backUpTheDirectory(String sourceDirPath, String zipFilePath) throws IOException {

    zipFilePath = zipFilePath + "wso2is-bak-" + System.currentTimeMillis() + ".zip";

    Path zipPath = Files.createFile(Paths.get(zipFilePath));
    try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zipPath))) {
        Path sourcePath = Paths.get(sourceDirPath);
        Files.walk(sourcePath).filter(path -> !Files.isDirectory(path)).forEach(path -> {
            ZipEntry zipEntry = new ZipEntry(sourcePath.relativize(path).toString());
            try {
                zipOutputStream.putNextEntry(zipEntry);
                Files.copy(path, zipOutputStream);
                zipOutputStream.closeEntry();
                log.info("Compressing: {}", path);
            } catch (IOException e) {
                log.error("Error while performing zip operation.", e);
            }/*  w w  w  .j a v a 2s  . c  om*/
        });
    }
}

From source file:adalid.util.io.FileWrapper.java

public Path getRelativePath(Path base) {
    if (path.startsWith(base)) {
        try {/* ww  w .  j  a v a2  s  .  c om*/
            return base.relativize(path);
        } catch (IllegalArgumentException ex) {
            return path;
        }
    } else {
        return path;
    }
}

From source file:fr.duminy.jbackup.core.archive.Compressor.java

public void compress(ArchiveParameters archiveParameters, List<SourceWithPath> files,
        final TaskListener listener, Cancellable cancellable) throws ArchiveException {
    final String name = archiveParameters.getArchive().toString();
    final MutableLong processedSize = new MutableLong();

    try (OutputStream fos = Files.newOutputStream(archiveParameters.getArchive());
            ArchiveOutputStream output = factory.create(fos)) {
        LOG.info("Backup '{}': creating archive {}", name, archiveParameters.getArchive());
        for (final SourceWithPath file : files) {
            if ((cancellable != null) && cancellable.isCancelled()) {
                break;
            }//  w ww.  j  a v  a2s .  co  m

            LOG.info("Backup '{}': compressing file {}", name, file.getPath().toAbsolutePath());
            try (InputStream input = createCountingInputStream(listener, processedSize,
                    Files.newInputStream(file.getPath()))) {
                final String path;
                if (archiveParameters.isRelativeEntries()) {
                    Path source = file.getSource();
                    if (Files.isDirectory(source)) {
                        if (source.getParent() == null) {
                            path = source.relativize(file.getPath()).toString();
                        } else {
                            path = source.getParent().relativize(file.getPath()).toString();
                        }
                    } else {
                        path = file.getPath().getFileName().toString();
                    }
                } else {
                    path = file.getPath().toString();
                }
                LOG.info("Backup '{}': adding entry {}", new Object[] { name, path });
                output.addEntry(path, input);
            }
        }
        LOG.info("Backup '{}': archive {} created ({})", new Object[] { name, archiveParameters.getArchive(),
                FileUtils.byteCountToDisplaySize(Files.size(archiveParameters.getArchive())) });
    } catch (IOException e) {
        throw new ArchiveException(e);
    } catch (Exception e) {
        throw new ArchiveException(e);
    }
}

From source file:org.openhab.tools.analysis.checkstyle.EshInfXmlValidationCheck.java

private void addToEshFiles(File xmlFile) {
    Path filePath = xmlFile.toPath();
    Path bundlePath = filePath.getParent().getParent().getParent();
    Path relativePath = bundlePath.relativize(filePath);
    eshInfFiles.put(relativePath, xmlFile);
}

From source file:nl.mpi.lamus.archive.implementation.LamusArchiveFileLocationProvider.java

/**
 * @see ArchiveFileLocationProvider#getChildPathRelativeToParent(java.io.File, java.lang.String)
 *//*w w  w.j  a  v a  2 s  . c  o m*/
@Override
public String getChildPathRelativeToParent(File parentNodeFile, File childNodeFile) {

    if (parentNodeFile.equals(childNodeFile)) {
        throw new IllegalStateException("Parent and child files should be different");
    }

    String parentDirectory = FilenameUtils.getFullPath(parentNodeFile.getAbsolutePath());
    Path parentDirPath = Paths.get(parentDirectory);
    Path childFilePath = Paths.get(childNodeFile.getAbsolutePath());
    Path relativePath = parentDirPath.relativize(childFilePath);

    return relativePath.toString();
}

From source file:de.tiqsolutions.hdfs.HadoopFileSystemPath.java

@Override
public Path getFileName() {
    Path p = getParent();
    return p == null ? null : p.relativize(this);
}

From source file:org.jephyr.common.maven.AbstractEnhanceMojo.java

@Override
public final void execute() throws MojoExecutionException {
    initialize();/*from   w  w  w.  j a v  a 2s  . co m*/

    File classesDirectory = getClassesDirectory();
    if (!classesDirectory.isDirectory()) {
        return;
    }

    Path classesPath = classesDirectory.toPath();
    Path outputPath = getOutputDirectory().toPath();

    for (File srcFile : listFiles(classesDirectory, null, true)) {
        Path relativePath = classesPath.relativize(srcFile.toPath());
        File destFile = outputPath.resolve(relativePath).toFile();
        if (srcFile.equals(destFile)) {
            if (shouldEnhance(separatorsToUnix(relativePath.toString()))) {
                enhance(srcFile, destFile);
            }
        } else if (srcFile.lastModified() > destFile.lastModified()) {
            if (shouldEnhance(separatorsToUnix(relativePath.toString()))) {
                enhance(srcFile, destFile);
            } else {
                try {
                    copyFile(srcFile, destFile);
                } catch (IOException e) {
                    throw new MojoExecutionException("Failed to copy " + srcFile + " to " + destFile, e);
                }
            }
        }
    }
}

From source file:org.roda.core.storage.fs.FSUtils.java

public static BinaryVersion convertPathToBinaryVersion(Path historyDataPath, Path historyMetadataPath,
        Path path) throws RequestNotValidException, NotFoundException, GenericException {
    DefaultBinaryVersion ret;/* w  w w . j  a v a 2 s  . co m*/

    if (!FSUtils.exists(path)) {
        throw new NotFoundException("Cannot find file version at " + path);
    }

    // storage path
    Path relativePath = historyDataPath.relativize(path);
    String fileName = relativePath.getFileName().toString();
    int lastIndexOfDot = fileName.lastIndexOf(VERSION_SEP);

    if (lastIndexOfDot <= 0 || lastIndexOfDot == fileName.length() - 1) {
        throw new RequestNotValidException("Bad name for versioned file: " + path);
    }

    String id = fileName.substring(lastIndexOfDot + 1);
    String realFileName = fileName.substring(0, lastIndexOfDot);
    Path realFilePath = relativePath.getParent().resolve(realFileName);
    Path metadataPath = historyMetadataPath
            .resolve(relativePath.getParent().resolve(fileName + METADATA_SUFFIX));

    StoragePath storagePath = FSUtils.getStoragePath(realFilePath);

    // construct
    ContentPayload content = new FSPathContentPayload(path);
    long sizeInBytes;
    try {
        sizeInBytes = Files.size(path);
        Map<String, String> contentDigest = null;
        Binary binary = new DefaultBinary(storagePath, content, sizeInBytes, false, contentDigest);

        if (FSUtils.exists(metadataPath)) {
            ret = JsonUtils.readObjectFromFile(metadataPath, DefaultBinaryVersion.class);
            ret.setBinary(binary);
        } else {
            Date createdDate = new Date(
                    Files.readAttributes(path, BasicFileAttributes.class).creationTime().toMillis());
            Map<String, String> defaultProperties = new HashMap<>();
            ret = new DefaultBinaryVersion(binary, id, createdDate, defaultProperties);
        }

    } catch (IOException e) {
        throw new GenericException("Could not get file size", e);
    }

    return ret;
}

From source file:nl.knaw.huygens.alexandria.dropwizard.cli.commands.AlexandriaCommand.java

private boolean isTagmlFile(final Path workDir, final Path filePath) {
    return workDir.relativize(filePath).startsWith(Paths.get(SOURCE_DIR))
            && fileType(filePath.toString()).equals(FileType.tagmlSource);
}

From source file:nl.knaw.huygens.alexandria.dropwizard.cli.commands.AlexandriaCommand.java

private boolean isViewDefinition(final Path workDir, final Path filePath) {
    return workDir.relativize(filePath).startsWith(Paths.get(VIEWS_DIR))
            && fileType(filePath.toString()).equals(FileType.viewDefinition);
}