如何在 Java 中制作不包含绝对路径名的 Zip/Jar

发布于 2025-01-05 21:15:42 字数 3358 浏览 2 评论 0原文

我正在用 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 变量是为了修改路径而创建的,但是在修改时,它给出了“无效的 .jar 文件”错误。 修改是这样的:

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();
    }
}

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

Instead of this:

.
|-- 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();
    }
}

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, ""));

Edit: 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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

口干舌燥 2025-01-12 21:15:42

要获取相对路径,您必须在调用 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(("\\", "/"));
...

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(("\\", "/"));
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文