如何在Java中创建一个不包含绝对路径名的Zip / Jar [英] How do you make a Zip/Jar in Java that will not contain the absolute pathname

查看:149
本文介绍了如何在Java中创建一个不包含绝对路径名的Zip / Jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm generating a .jar file in Java, but the .jar contains an absolute pathname of where it is in the system (/tmp/tempXXX/foo instead of /foo). The tree is like this:

.
|-- META-INF
|-|- ....
|-- tmp
|-|- tempXXX
|-|-|- foo
|-|-|- bar

而不是:

.
|-- META-INF
|-|- ....
|-- foo
|-- bar

有可能解决这个问题吗?以下是使它成为的函数:

Is it possible to fix this? Here is the function that makes it:

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变量用于修改路径,但是修改时,它给出了无效的.jar文件错误。
修改是:

The source2 variable was made for modifying the path, but when modifying, it gave an "Invalid .jar file" error. The modification was this:

File source2 = new File(source.getPath().replaceAll("^" + removeme, ""));

编辑:现在可以使用了。如果有人有兴趣,这是新代码:

It works now. Here is the new code if anyone is interested:

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)时必须提供相对路径。尝试删除路径的一部分直到父目录。所以这就像是,

To get the relative path, you have to provide a relative path when calling JarEntry(name). Try removing the portion of the path up to the parent directory. So that would be something like,

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(("\\", "/"));
...

这篇关于如何在Java中创建一个不包含绝对路径名的Zip / Jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆