Linux 有没有一种快速的方法可以从 Jar/war 中删除文件而不必提取 jar 并重新创建它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4520822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a quick way to delete a file from a Jar / war without having to extract the jar and recreate it?
提问by kellyfj
So I need to remove a file from a jar / war file. I was hoping there was something like "jar -d myjar.jar file_I_donot_need.txt"
所以我需要从 jar/war 文件中删除一个文件。我希望有类似“jar -d myjar.jar file_I_donot_need.txt”的东西
But right now the only way I can see of doing this from my Linux command line (without using WinRAR/Winzip or linux equivalent) is to
但是现在我可以从我的 Linux 命令行(不使用 WinRAR/Winzip 或 linux 等效项)看到这样做的唯一方法是
- Do "jar -xvf" and extract the
complete Jar file - Remove the file(s) I don't need
- Rejar the jar file using "jar -cvf"
- 执行“jar -xvf”并提取
完整的 Jar 文件 - 删除我不需要的文件
- 使用“jar -cvf”重新jar文件
Please tell me there is a shorter way?
请告诉我有更短的方法吗?
采纳答案by martona
zip -d file.jar unwanted_file.txt
jar is just a zip file after all. Definitely much faster than uncompressing/recompressing.
jar 毕竟只是一个 zip 文件。绝对比解压缩/重新压缩快得多。
回答by Peter Lawrey
In Java you can copy all the entries of a jar except the one you want to delete. i.e. you have to make a copy but don't need to create the individual files.
在 Java 中,您可以复制 jar 的所有条目,但要删除的条目除外。即您必须制作副本但不需要创建单个文件。
You can do this by
你可以这样做
- creating a new jar.
- iterating though the Jar you have
- copy the entry from one jar to the other, skipping any files you want.
- close and replace the orginal jar if you want.
- 创建一个新罐子。
- 迭代你拥有的 Jar
- 将条目从一个 jar 复制到另一个,跳过您想要的任何文件。
- 如果需要,关闭并更换原始罐子。
回答by Tomá? Zálusky
In case you want to delete file in order to unsign signed jar, you can probably just make the .RSA file zero-sized. This can be accomplished with just jar u
. See https://stackoverflow.com/a/24678645/653539. (Worked for me, though I admit it's hack.)
如果您想删除文件以取消签名的 jar,您可以将 .RSA 文件设为零大小。这可以通过jar u
. 请参阅https://stackoverflow.com/a/24678645/653539。(为我工作,虽然我承认这是黑客。)