WAR 存档中的 Java 编辑文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16930284/
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
Java edit file inside WAR archive
提问by Leo Hui
I've been reading on forums on how to read/edit files inside archive but still cannot get my answer. Mainly using getResourceAsStream, and must be within its classpath.
我一直在论坛上阅读有关如何阅读/编辑存档中的文件的信息,但仍然无法得到我的答案。主要使用 getResourceAsStream,并且必须在其类路径中。
What I need to do is, read and update the xml file with a given filepath as input, then redeploy it to Tomcat. But so far I still can't get answer to how to edit the xml files inside a AAR file within a WAR archive with a given full path as input. Can someone help me please?
我需要做的是,使用给定的文件路径作为输入读取并更新 xml 文件,然后将其重新部署到 Tomcat。但是到目前为止,我仍然无法回答如何使用给定的完整路径作为输入来编辑 WAR 存档中的 AAR 文件中的 xml 文件。有人能帮助我吗?
for example, I would like to edit the applicationContext.xml file:
例如,我想编辑 applicationContext.xml 文件:
C:/webapp.WAR
C:/webapp.WAR
From the webapp.WAR file:
从 webapp.WAR 文件:
webapp.WAR/WEB-INF/services/myapp.AAR
webapp.WAR/WEB-INF/services/myapp.AAR
From the myapp.AAR:
从 myapp.AAR:
myapp.AAR/applicationContext.xml
myapp.AAR/applicationContext.xml
回答by Ryan Stewart
You can't modify files contained in any ?AR file (WAR, JAR, EAR, AAR, ...). It's basically a zip archive, and the only way to modify it is to unzip it, make the changes, and zip it up again.
您不能修改任何 ?AR 文件(WAR、JAR、EAR、AAR 等)中包含的文件。它基本上是一个 zip 存档,修改它的唯一方法是解压缩它,进行更改,然后再次压缩它。
If you're trying to have a running application modify itself, then recognize that 1) many containers run from an exploded copy of an ?AR file for various reasons, and 2) it's unlikely to do you any good to modify files on the fly anyway unless you plan to restart the application after the change or write lots of code to monitor for changes and refresh your app in some way afterward. Either way, you're probably better off figuring out how to make your desired change programmatically than trying to rewrite a running application.
如果您试图让正在运行的应用程序自行修改,那么请认识到 1) 许多容器出于各种原因从 ?AR 文件的分解副本运行,以及 2) 动态修改文件不太可能对您有任何好处无论如何,除非您计划在更改后重新启动应用程序或编写大量代码来监视更改并在之后以某种方式刷新您的应用程序。无论哪种方式,与尝试重写正在运行的应用程序相比,您最好弄清楚如何以编程方式进行所需的更改。
On the other hand, if you're not talking about having an application modify itself but instead changing a WAR file and then deploying the new version, it's just what I said above. Explode it using the jar
tool, modify the exploded directory, and then compress it again with jar
. Then deploy it like a new war file.
另一方面,如果您不是在谈论让应用程序修改自身,而是在更改 WAR 文件然后部署新版本,那么这就是我上面所说的。使用jar
工具解压,修改解压后的目录,然后再次压缩jar
。然后像部署一个新的war文件一样部署它。
回答by Rukmani Vijayakumar
You can edit a war as follow,
//You can loop through your war file using the following code
ZipFile warFile = new ZipFile( warFile );
for( Enumeration e = warFile.entries(); e.hasMoreElements(); )
{
ZipEntry entry = (ZipEntry) e.nextElement();
if( entry.getName().contains( yourXMLFile ) )
{
//read your xml file
File fXmlFile = new File( entry );
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
/**Now write your xml file to another file and save it say, createXMLFile **/
//Appending the newly created xml file and
//deleting the old one.
Map<String, String> zip_properties = new HashMap<>();
zip_properties.put("create", "false");
zip_properties.put("encoding", "UTF-8");
URI uri = URI.create( "jar:" + warFile.toUri() );
try( FileSystem zipfs = FileSystems.newFileSystem(uri, zip_properties) ) {
Path yourXMLFile = zipfs.getPath( yourXMLFile );
Path tempyourXMLFile = yourXMLFile;
Files.delete( propertyFilePathInWar );
//Path where the file to be added resides
Path addNewFile = Paths.get( createXMLFile );
//Append file to war File
Files.copy(addNewFile, tempyourXMLFile);
zipfs.close();
}
}
}