你如何在 Java 中制作一个不包含绝对路径名的 Zip/Jar [英] How do you make a Zip/Jar in Java that will not contain the absolute pathname
问题描述
我正在用 Java 生成一个 .jar 文件,但 .jar 包含它在系统中的位置的绝对路径名(/tmp/tempXXX/foo 而不是/foo).树是这样的:
<预><代码>.|-- 元信息|-|- ....|-- tmp|-|- tempXXX|-|-|- foo|-|-|- 酒吧取而代之的是:
<预><代码>.|-- 元信息|-|- ....|-- 富|-- 酒吧有没有可能解决这个问题?这是实现它的功能:
public static void add(File source, JarOutputStream target, String removeme)抛出 IOException{BufferedInputStream in = null;尝试{文件源 2 = 源;如果 (source.isDirectory()){String name = source2.getPath().replace("\", "/");如果 (!name.isEmpty()){if (!name.endsWith("/"))名称 += "/";JarEntry entry = new JarEntry(name);entry.setTime(source.lastModified());target.putNextEntry(entry);target.closeEntry();}for (文件嵌套文件:source.listFiles())添加(嵌套文件,目标,removeme);返回;}JarEntry entry = new JarEntry(source2.getPath().replace("\", "/"));entry.setTime(source.lastModified());target.putNextEntry(entry);in = new BufferedInputStream(new FileInputStream(source));字节[]缓冲区=新字节[2048];而(真){int count = in.read(buffer);如果(计数== -1)休息;target.write(buffer, 0, count);}target.closeEntry();}最后{如果(在!= null)附寄();}}
source2 变量是为了修改路径而制作的,但是修改的时候报错Invalid .jar file".修改是这样的:
File source2 = new File(source.getPath().replaceAll("^" + removeme, ""));
现在可以使用了.如果有人感兴趣,这是新代码:
public static void add(File source, JarOutputStream target, String removeme)抛出 IOException{BufferedInputStream in = null;尝试{File parentDir = new File(removeme);File source2 = new File(source.getCanonicalPath().substring(parentDir.getCanonicalPath().length() + 1,source.getCanonicalPath().length()));如果 (source.isDirectory()){String name = source2.getPath().replace("\", "/");如果 (!name.isEmpty()){if (!name.endsWith("/"))名称 += "/";JarEntry entry = new JarEntry(name);entry.setTime(source.lastModified());target.putNextEntry(entry);target.closeEntry();}for (文件嵌套文件:source.listFiles())添加(嵌套文件,目标,removeme);返回;}JarEntry entry = new JarEntry(source2.getPath().replace("\", "/"));entry.setTime(source.lastModified());target.putNextEntry(entry);in = new BufferedInputStream(new FileInputStream(source));字节[]缓冲区=新字节[2048];而(真){int count = in.read(buffer);如果(计数== -1)休息;target.write(buffer, 0, count);}target.closeEntry();}最后{如果(在!= null)附寄();}}
要获取相对路径,调用 JarEntry(name) 时必须提供相对路径.尝试删除直到父目录的路径部分.所以这会是这样的,
File parentDir = "src";//你想要的相对路径的目录字符串 relPath = source.getCanonicalPath().substring(parentDir.getCanonicalPath().length() + 1,source.getCanonicalPath().length());JarEntry entry = new JarEntry(relPath.replace(("\", "/"));...
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();
}
}
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屋!