使用 Java 将文件附加到 zip 文件 [英] Appending files to a zip file with Java

查看:46
本文介绍了使用 Java 将文件附加到 zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在提取一个 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 以编程方式完成的 - 但我想知道复制战争文件然后只是附加文件是否会更有效 - 那么只要战争扩大并且我就不必等待然后必须再次压缩.

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 文件系统,允许在 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 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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