如何更新预先存在的Jar文件

时间:2023-01-13 21:51:50

I've got a WAR file that I need to add two files to. Currently, I'm doing this:

我有一个WAR文件,我需要添加两个文件。目前,我这样做:

File war = new File(DIRECTORY, "server.war");
JarOutputStream zos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(war)));

//Add file 1
File file = new File(DIRECTORY, "file1.jar");
InputStream is = new BufferedInputStream(new FileInputStream(file));
ZipEntry e = new ZipEntry("file1.jar");
zos.putNextEntry(e);
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf, 0, buf.length)) != -1) {
    zos.write(buf, 0, len);
}
is.close();
zos.closeEntry();

//repeat for file 2

zos.close();

The result is that the previous contents get clobbered: the WAR has only the 2 files I just added in it. Is there some sort of append mode that I'm not using or what?

结果是先前的内容被破坏了:WAR只有我刚刚添加的2个文件。是否有某种追加模式,我没有使用或什么?

3 个解决方案

#1


6  

Yeah, there's an extra boolean argument to the FileOutputStream constructor which lets you force it to append to the file rather than overwrite it. Change your code to

是的,FileOutputStream构造函数有一个额外的布尔参数,它允许你强制它附加到文件而不是覆盖它。将您的代码更改为

JarOutputStream zos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(war, True)));

and it should work the way you want.

它应该按照你想要的方式工作。

#2


2  

It seems this can't be done. I thought it was for a while, but it seems that it wasn't quite having the effect I wanted. Doing it this way resulted in the equivalent of two separate jar files concatinated together. The weird part was that the tools were making some sense of it. JAR found the first, original jar file and read me that. Glassfish's classloader was finding the later, new part, resulting in it loading only the added files as if they were all of the app. Weird.

这似乎无法做到。我认为这已经有一段时间了,但似乎它并没有达到我想要的效果。这样做会导致相当于两个单独的jar文件连在一起。奇怪的是,工具对它有所了解。 JAR找到了第一个原始的jar文件并读了我一遍。 Glassfish的类加载器正在寻找后来的新部件,导致它只加载添加的文件,就好像它们都是应用程序一样。奇怪的。

So I've resurted to creating a new war, adding the contents of the old, adding the new files, closing, and copying the new over the old.

所以我决定创建一个新的战争,添加旧的内容,添加新文件,关闭,并复制旧的旧。

#3


0  

I have the same problem; I'm looking for an easy way to update a file in an existing jar file. If it's so easy to do a "jar uf foo.jar ..." command, how come there isn't a way to use Java API's to do the same?

我也有同样的问题;我正在寻找一种简单的方法来更新现有jar文件中的文件。如果执行“jar uf foo.jar ...”命令很容易,为什么没有办法使用Java API来做同样的事情呢?

Anyway, here is the RFE to add this functionality to Java; it also suggests some work-arounds:

无论如何,这是将此功能添加到Java的RFE;它还提出了一些解决方法:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4129445

And here is a library that purports to make this a lot easier, by treating JARs/ZIPs like virtual directories:

这里有一个库,声称通过处理像虚拟目录这样的JAR / ZIP来使这更容易:

https:// truezip.dev.java.net

I haven't figured out yet which approach I'm going to use for my current problem.

我还没有弄清楚我将使用哪种方法解决当前的问题。

#1


6  

Yeah, there's an extra boolean argument to the FileOutputStream constructor which lets you force it to append to the file rather than overwrite it. Change your code to

是的,FileOutputStream构造函数有一个额外的布尔参数,它允许你强制它附加到文件而不是覆盖它。将您的代码更改为

JarOutputStream zos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(war, True)));

and it should work the way you want.

它应该按照你想要的方式工作。

#2


2  

It seems this can't be done. I thought it was for a while, but it seems that it wasn't quite having the effect I wanted. Doing it this way resulted in the equivalent of two separate jar files concatinated together. The weird part was that the tools were making some sense of it. JAR found the first, original jar file and read me that. Glassfish's classloader was finding the later, new part, resulting in it loading only the added files as if they were all of the app. Weird.

这似乎无法做到。我认为这已经有一段时间了,但似乎它并没有达到我想要的效果。这样做会导致相当于两个单独的jar文件连在一起。奇怪的是,工具对它有所了解。 JAR找到了第一个原始的jar文件并读了我一遍。 Glassfish的类加载器正在寻找后来的新部件,导致它只加载添加的文件,就好像它们都是应用程序一样。奇怪的。

So I've resurted to creating a new war, adding the contents of the old, adding the new files, closing, and copying the new over the old.

所以我决定创建一个新的战争,添加旧的内容,添加新文件,关闭,并复制旧的旧。

#3


0  

I have the same problem; I'm looking for an easy way to update a file in an existing jar file. If it's so easy to do a "jar uf foo.jar ..." command, how come there isn't a way to use Java API's to do the same?

我也有同样的问题;我正在寻找一种简单的方法来更新现有jar文件中的文件。如果执行“jar uf foo.jar ...”命令很容易,为什么没有办法使用Java API来做同样的事情呢?

Anyway, here is the RFE to add this functionality to Java; it also suggests some work-arounds:

无论如何,这是将此功能添加到Java的RFE;它还提出了一些解决方法:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4129445

And here is a library that purports to make this a lot easier, by treating JARs/ZIPs like virtual directories:

这里有一个库,声称通过处理像虚拟目录这样的JAR / ZIP来使这更容易:

https:// truezip.dev.java.net

I haven't figured out yet which approach I'm going to use for my current problem.

我还没有弄清楚我将使用哪种方法解决当前的问题。