Example usage for java.net URI relativize

List of usage examples for java.net URI relativize

Introduction

In this page you can find the example usage for java.net URI relativize.

Prototype

public URI relativize(URI uri) 

Source Link

Document

Relativizes the given URI against this URI.

Usage

From source file:org.deegree.commons.utils.io.Zip.java

/**
 * .svn files/directories will be ignored.
 * //from ww  w  . j  ava2  s. c  o m
 * @param f
 * @param out
 * @param parent
 *            may be null, all written paths in the zip will be relative to this one (default is f.toURI)
 * @throws IOException
 */
public static void zip(File f, ZipOutputStream out, URI parent) throws IOException {
    if (f.getName().equalsIgnoreCase(".svn")) {
        return;
    }

    if (parent == null) {
        parent = f.toURI();
    }

    String name = parent.relativize(f.getAbsoluteFile().toURI()).toString();
    if (f.isDirectory()) {
        if (!name.isEmpty()) {
            ZipEntry e = new ZipEntry(name);
            out.putNextEntry(e);
        }
        File[] fs = f.listFiles();
        if (fs != null) {
            for (File f2 : fs) {
                zip(f2, out, parent);
            }
        }
    } else {
        ZipEntry e = new ZipEntry(name);
        out.putNextEntry(e);
        InputStream is = null;
        try {
            is = new FileInputStream(f);
            copy(is, out);
        } finally {
            closeQuietly(is);
        }
    }
}

From source file:Main.java

private static void zipDir(String dir, ZipOutputStream out) throws IOException {
    File directory = new File(dir);

    URI base = directory.toURI();

    ArrayList<File> filesToZip = new ArrayList<File>();

    GetFiles(directory, filesToZip);/*from w ww . ja va  2 s . co  m*/

    for (int i = 0; i < filesToZip.size(); ++i) {
        FileInputStream in = new FileInputStream(filesToZip.get(i));

        String name = base.relativize(filesToZip.get(i).toURI()).getPath();

        out.putNextEntry(new ZipEntry(name));

        byte[] buf = new byte[4096];
        int bytes = 0;

        while ((bytes = in.read(buf)) != -1) {
            out.write(buf, 0, bytes);
        }

        out.closeEntry();

        in.close();
    }

    out.finish();
    out.flush();
}

From source file:org.eclipse.smarthome.test.SyntheticBundleInstaller.java

private static String convertToFileEntry(URI baseURI, URL entryURL) throws URISyntaxException {
    URI entryURI = entryURL.toURI();
    URI relativeURI = baseURI.relativize(entryURI);
    String fileEntry = relativeURI.toString();
    return fileEntry;
}

From source file:org.deegree.maven.utils.ZipUtils.java

public static void zip(File f, ZipOutputStream out, URI parent, Set<String> visitedFiles) throws Throwable {
    if (f.getName().equalsIgnoreCase(".svn") || f.getName().equalsIgnoreCase("CVS")) {
        return;//  w ww.j a  va 2 s  .  c om
    }

    if (parent == null) {
        parent = f.toURI();
    }

    String name = parent.relativize(f.getAbsoluteFile().toURI()).toString();

    if (f.isDirectory()) {
        zipDirectory(name, visitedFiles, out, parent, f);
    } else {
        if (!visitedFiles.contains(name)) {
            visitedFiles.add(name);
            ZipEntry e = new ZipEntry(name);
            try {
                out.putNextEntry(e);
            } catch (Throwable ex) {
                // probably duplicate entry
            }
            InputStream is = null;
            try {
                is = new FileInputStream(f);
                copy(is, out);
            } finally {
                closeQuietly(is);
            }
        }
    }
}

From source file:org.apache.maven.wagon.shared.http.HtmlFileListParser.java

private static String cleanLink(URI baseURI, String link) {
    if (StringUtils.isEmpty(link)) {
        return "";
    }/*from  w  w  w .  ja va  2s. co  m*/

    String ret = link;

    try {
        URI linkuri = new URI(ret);
        if (link.startsWith("/")) {
            linkuri = baseURI.resolve(linkuri);
        }
        URI relativeURI = baseURI.relativize(linkuri).normalize();
        ret = relativeURI.toASCIIString();
        if (ret.startsWith(baseURI.getPath())) {
            ret = ret.substring(baseURI.getPath().length());
        }

        ret = URLDecoder.decode(ret, "UTF-8");
    } catch (URISyntaxException e) {
    } catch (UnsupportedEncodingException e) {
    }

    return ret;
}

From source file:org.interreg.docexplore.util.ZipUtils.java

static int zip(File[] files, Deque<File> queue, URI base, int cnt, int nEntries, float[] progress,
        float progressOffset, float progressAmount, ArchiveOutputStream zout) throws IOException {
    for (File kid : files) {
        String name = base.relativize(kid.toURI()).getPath();
        if (kid.isDirectory()) {
            queue.push(kid);//from   ww  w .j  av  a 2  s  .c o  m
            name = name.endsWith("/") ? name : name + "/";
            ArchiveEntry entry = zout.createArchiveEntry(kid, name);
            zout.putArchiveEntry(entry);
            zout.closeArchiveEntry();
        } else {
            ArchiveEntry entry = zout.createArchiveEntry(kid, name);
            zout.putArchiveEntry(entry);
            copy(kid, zout);
            zout.closeArchiveEntry();

            cnt++;
            if (progress != null)
                progress[0] = progressOffset + cnt * progressAmount / nEntries;
        }
    }
    return cnt;
}

From source file:org.identityconnectors.test.common.TestHelpers.java

/**
 * Method for convenient testing of local connectors.
 *///from   w  ww .  ja va  2  s  .c o  m
public static APIConfiguration createTestConfiguration(Class<? extends Connector> clazz,
        final PropertyBag configData, String prefix) {
    URL url = clazz.getClassLoader().getResource("");
    Set<String> bundleContents = new HashSet<String>();

    try {
        URI relative = url.toURI();
        for (File file : FileUtils.listFiles(new File(url.toURI()), TrueFileFilter.INSTANCE,
                TrueFileFilter.INSTANCE)) {
            bundleContents.add(relative.relativize(file.toURI()).getPath());
        }
        return getSpi().createTestConfiguration(clazz, bundleContents, configData, prefix);
    } catch (Exception e) {
        throw ConnectorException.wrap(e);
    }
}

From source file:org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer.java

/**
 * /*  w  ww  .  ja va2s .  c  o m*/
 * @param fileEntry the fileEntry, must not be {@code null}
 * @param path the path, must not be {@code null}
 * @return
 */
public static URI resolveURI(String fileEntry, URI path) {
    // When using URI.create, spaces need to be escaped but not the slashes, so we can't use
    // URLEncoder.encode( String, String )
    // new File( String ).toURI() results in an absolute URI while path is relative, so that can't be used either.
    return path.relativize(URI.create(stripQuotes(fileEntry).replace(" ", "%20")));
}

From source file:org.roda.core.util.ZipUtility.java

public static void zip(File directory, ZipOutputStream zout) throws IOException {
    URI base = directory.toURI();
    Deque<File> queue = new LinkedList<>();
    queue.push(directory);//from   w  w w .  jav  a  2  s .  c  o m
    try {
        while (!queue.isEmpty()) {
            File dir = queue.pop();
            for (File kid : dir.listFiles()) {
                String name = base.relativize(kid.toURI()).getPath();
                if (kid.isDirectory()) {
                    queue.push(kid);
                    name = name.endsWith("/") ? name : name + "/";
                    zout.putNextEntry(new ZipEntry(name));
                } else {
                    zout.putNextEntry(new ZipEntry(name));
                    copy(kid, zout);
                    zout.closeEntry();
                }
            }
        }
    } finally {
        zout.close();
    }
}

From source file:org.hyperledger.fabric.sdk.helper.Utils.java

/**
 * Compress the contents of given directory using Tar and Gzip to an in-memory byte array.
 *
 * @param sourceDirectory  the source directory.
 * @param pathPrefix       a path to be prepended to every file name in the .tar.gz output, or {@code null} if no prefix is required.
 * @param chaincodeMetaInf/*ww w .  j a  v a 2s .  c  o  m*/
 * @return the compressed directory contents.
 * @throws IOException
 */
public static byte[] generateTarGz(File sourceDirectory, String pathPrefix, File chaincodeMetaInf)
        throws IOException {
    logger.trace(format("generateTarGz: sourceDirectory: %s, pathPrefix: %s, chaincodeMetaInf: %s",
            sourceDirectory == null ? "null" : sourceDirectory.getAbsolutePath(), pathPrefix,
            chaincodeMetaInf == null ? "null" : chaincodeMetaInf.getAbsolutePath()));

    ByteArrayOutputStream bos = new ByteArrayOutputStream(500000);

    String sourcePath = sourceDirectory.getAbsolutePath();

    TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(
            new GzipCompressorOutputStream(bos));
    archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    try {
        Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true);

        ArchiveEntry archiveEntry;
        FileInputStream fileInputStream;
        for (File childFile : childrenFiles) {
            String childPath = childFile.getAbsolutePath();
            String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length());

            if (pathPrefix != null) {
                relativePath = Utils.combinePaths(pathPrefix, relativePath);
            }

            relativePath = FilenameUtils.separatorsToUnix(relativePath);

            if (TRACE_ENABED) {
                logger.trace(format("generateTarGz: Adding '%s' entry from source '%s' to archive.",
                        relativePath, childFile.getAbsolutePath()));
            }

            archiveEntry = new TarArchiveEntry(childFile, relativePath);
            fileInputStream = new FileInputStream(childFile);
            archiveOutputStream.putArchiveEntry(archiveEntry);

            try {
                IOUtils.copy(fileInputStream, archiveOutputStream);
            } finally {
                IOUtils.closeQuietly(fileInputStream);
                archiveOutputStream.closeArchiveEntry();
            }

        }

        if (null != chaincodeMetaInf) {
            childrenFiles = org.apache.commons.io.FileUtils.listFiles(chaincodeMetaInf, null, true);

            final URI metabase = chaincodeMetaInf.toURI();

            for (File childFile : childrenFiles) {

                final String relativePath = Paths
                        .get("META-INF", metabase.relativize(childFile.toURI()).getPath()).toString();

                if (TRACE_ENABED) {
                    logger.trace(format("generateTarGz: Adding '%s' entry from source '%s' to archive.",
                            relativePath, childFile.getAbsolutePath()));
                }

                archiveEntry = new TarArchiveEntry(childFile, relativePath);
                fileInputStream = new FileInputStream(childFile);
                archiveOutputStream.putArchiveEntry(archiveEntry);

                try {
                    IOUtils.copy(fileInputStream, archiveOutputStream);
                } finally {
                    IOUtils.closeQuietly(fileInputStream);
                    archiveOutputStream.closeArchiveEntry();
                }

            }

        }
    } finally {
        IOUtils.closeQuietly(archiveOutputStream);
    }

    return bos.toByteArray();
}