问题描述
我目前正在提取war文件的内容,然后将一些新文件添加到目录结构中,然后创建一个新的war文件。
I am currently extracting the contents of a war file and then adding some new files to the directory structure and then creating a new war file.
这一切都已完成从Java编程 - 但我想知道复制war文件然后只是附加文件是否更有效 - 那么只要战争扩展然后必须再次压缩,我就不必等待。
This is all done programatically from Java - but I am wondering if it wouldn't be more efficient to copy the war file and then just append the files - then I wouldn't have to wait so long as the war expands and then has to be compressed again.
我似乎无法在文档或任何在线示例中找到这样做的方法。
I can't seem to find a way to do this in the documentation though or any online examples.
任何人都可以给出一些提示或指示?
Anyone can give some tips or pointers?
更新:
其中一个答案中提到的TrueZip似乎成为一个非常好的java库附加到一个zip文件(尽管其他答案说它不可能这样做)。
TrueZip as mentioned in one of the answers seems to be a very good java library to append to a zip file (despite other answers that say it is not possible to do this).
任何人都有关于TrueZip的经验或反馈或者可以推荐其他类似的库?
Anyone have experience or feedback on TrueZip or can recommend other similar libaries?
推荐答案
在Java 7中,我们得到了,允许在zip(jar,war)中添加和更改文件,无需手动重新打包。
In Java 7 we got Zip File System that allows adding and changing files in zip (jar, war) without manual repackaging.
我们可以直接写入zip文件中的文件,如下例所示。
We can directly write to files inside zip files as in the following example.
Map<String, String> env = new HashMap<>();
env.put("create", "true");
Path path = Paths.get("test.zip");
URI uri = URI.create("jar:" + path.toUri());
try (FileSystem fs = FileSystems.newFileSystem(uri, env))
{
Path nf = fs.getPath("new.txt");
try (Writer writer = Files.newBufferedWriter(nf, StandardCharsets.UTF_8, StandardOpenOption.CREATE)) {
writer.write("hello");
}
}
这篇关于使用Java将文件附加到zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!