Java Path Relative Get getRelativePath(String rootpath, String path)

Here you can find the source of getRelativePath(String rootpath, String path)

Description

get Relative Path

License

Open Source License

Declaration

public static String getRelativePath(String rootpath, String path) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007 Exadel, Inc. and Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  ww w  .j  a  va  2 s  .c  om*/
 *     Exadel, Inc. and Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.StringTokenizer;
import java.util.jar.JarEntry;

import java.util.jar.JarOutputStream;

public class Main {
    public static String getRelativePath(String rootpath, String path) {
        String[] r = tokenizePath(rootpath);
        String[] p = tokenizePath(path);
        if (r.length == 0 || p.length == 0 || !r[0].equalsIgnoreCase(p[0]))
            return null;
        int i = 0;
        while (i < r.length && i < p.length && r[i].equalsIgnoreCase(p[i]))
            ++i;
        StringBuffer sb = new StringBuffer();
        for (int k = i; k < r.length; k++)
            sb.append("/..");
        for (int k = i; k < p.length; k++)
            sb.append("/").append(p[k]);
        return sb.toString();
    }

    private static String[] tokenizePath(String path) {
        String tokenizedPath = path.replace('\\', '/');
        StringTokenizer st = new StringTokenizer(tokenizedPath, "/");
        ArrayList l = new ArrayList();
        while (st.hasMoreTokens()) {
            String t = st.nextToken();
            if (t.length() == 0 || t.equals("."))
                continue;
            if (t.equals("..")) {
                if (l.size() > 0)
                    l.remove(l.size() - 1);
                continue;
            }
            l.add(t);
        }
        return (String[]) l.toArray(new String[0]);
    }

    public static void remove(File f) {
        if (f.isFile())
            f.delete();
        if (!f.isDirectory())
            return;
        File[] fs = f.listFiles();
        if (fs != null)
            for (int i = 0; i < fs.length; i++)
                remove(fs[i]);
        f.delete();
    }

    public static void add(File root, File f, JarOutputStream jos) throws IOException {
        int l = root.getAbsolutePath().length();
        String en = f.getAbsolutePath().substring(l + 1).replace('\\', '/');
        add(f, en, jos);
    }

    public static void add(File f, String name, JarOutputStream jos) throws IOException {
        String en = name;
        if (f.isDirectory())
            en += "/";
        JarEntry entry = (en.endsWith("/")) ? null : new JarEntry(en);
        if (f.isDirectory()) {
            if ("/".equals(en))
                en = "";
            File[] fs = f.listFiles();
            if (fs != null)
                for (int i = 0; i < fs.length; i++)
                    add(fs[i], en + fs[i].getName(), jos);
        } else {
            try {
                jos.putNextEntry(entry);
            } catch (IOException e) {
                return;
            }
            FileInputStream is = new FileInputStream(f);
            byte[] b = new byte[1024];
            int q = 0;
            while ((q = is.available()) > 0) {
                if (q > 1024)
                    q = 1024;
                q = is.read(b, 0, q);
                jos.write(b, 0, q);
            }
            is.close();
        }
        if (entry != null)
            jos.closeEntry();
    }
}

Related

  1. getRelativePath(String filePath, String basePath)
  2. getRelativePath(String from, String to)
  3. getRelativePath(String parent, String child)
  4. getRelativePath(String path, String base)
  5. getRelativePath(String root, String path)
  6. getRelativePath(String source, String target)
  7. getRelativePath(String target, String base)
  8. getRelativePath(String targetPath, String basePath, String pathSeparator)
  9. getRelativePath(String targetPath, String basePath, String pathSeparator)