如何在Java中创建一个不包含绝对路径名的Zip/Jar

如何在Java中创建一个不包含绝对路径名的Zip/Jar,java,path,jar,absolute,Java,Path,Jar,Absolute,我正在用Java生成一个.jar文件,但是.jar包含它在系统中的位置的绝对路径名(/tmp/tempXXX/foo而不是/foo)。 这棵树是这样的: . |-- META-INF |-|- .... |-- tmp |-|- tempXXX |-|-|- foo |-|-|- bar 与此相反: . |-- META-INF |-|- .... |-- foo |-- bar 有可能解决这个问题吗?以下是它的功能: public static void add(File source,

我正在用Java生成一个.jar文件,但是.jar包含它在系统中的位置的绝对路径名(/tmp/tempXXX/foo而不是/foo)。 这棵树是这样的:

.
|-- META-INF
|-|- ....
|-- tmp
|-|- tempXXX
|-|-|- foo
|-|-|- bar
与此相反:

.
|-- META-INF
|-|- ....
|-- foo
|-- bar
有可能解决这个问题吗?以下是它的功能:

public static void add(File source, JarOutputStream target, String removeme)
        throws IOException
{
    BufferedInputStream in = null;
    try
    {
        File source2 = source;
        if (source.isDirectory())
        {
            String name = source2.getPath().replace("\\", "/");
            if (!name.isEmpty())
            {
                if (!name.endsWith("/"))
                    name += "/";
                JarEntry entry = new JarEntry(name);
                entry.setTime(source.lastModified());
                target.putNextEntry(entry);
                target.closeEntry();
            }
            for (File nestedFile : source.listFiles())
                add(nestedFile, target, removeme);
            return;
        }

        JarEntry entry = new JarEntry(source2.getPath().replace("\\", "/"));
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        in = new BufferedInputStream(new FileInputStream(source));

        byte[] buffer = new byte[2048];
        while (true)
        {
            int count = in.read(buffer);
            if (count == -1)
                break;
            target.write(buffer, 0, count);
        }
        target.closeEntry();
    }
    finally
    {
        if (in != null)
            in.close();
    }
}
source2变量用于修改路径,但在修改时,它给出了一个“Invalid.jar file”错误。 修改如下:

File source2 = new File(source.getPath().replaceAll("^" + removeme, ""));
编辑:现在可以了。如果有人感兴趣,以下是新代码:

public static void add(File source, JarOutputStream target, String removeme)
        throws IOException
{
    BufferedInputStream in = null;
    try
    {
        File parentDir = new File(removeme);
        File source2 = new File(source.getCanonicalPath().substring(
                parentDir.getCanonicalPath().length() + 1,
                source.getCanonicalPath().length()));
        if (source.isDirectory())
        {
            String name = source2.getPath().replace("\\", "/");
            if (!name.isEmpty())
            {
                if (!name.endsWith("/"))
                    name += "/";
                JarEntry entry = new JarEntry(name);
                entry.setTime(source.lastModified());
                target.putNextEntry(entry);
                target.closeEntry();
            }
            for (File nestedFile : source.listFiles())
                add(nestedFile, target, removeme);
            return;
        }

        JarEntry entry = new JarEntry(source2.getPath().replace("\\", "/"));
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        in = new BufferedInputStream(new FileInputStream(source));

        byte[] buffer = new byte[2048];
        while (true)
        {
            int count = in.read(buffer);
            if (count == -1)
                break;
            target.write(buffer, 0, count);
        }
        target.closeEntry();
    }
    finally
    {
        if (in != null)
            in.close();
    }
}

要获得相对路径,必须在调用JarEntry(name)时提供相对路径。尝试删除指向父目录的路径部分。那大概是

File parentDir = "src";//dir from which you want the relative path

String relPath = source.getCanonicalPath()
                  .substring(parentDir.getCanonicalPath().length() + 1,
                             source.getCanonicalPath().length());

JarEntry entry = new JarEntry(relPath.replace(("\\", "/"));
...