2011-06-07 125 views
1

我知道如何用Java创建一个临时目录,但是有没有简单的方法将Java文件中的文件从jar文件复制到这个目录?java:如何将jar文件中的目录复制到tmp目录中?

File tmpDir = new File(System.getProperty("java.io.tmpdir")); 
File helpDir = new File(tmpDir, "myApp-help"); 
helpDir.createNewFile(); // oops, this isn't right, I want to create a dir 
URL helpURL = getClass().getResource("/help-info"); 

/* ???? I want to copy the files from helpURL to helpDir */ 

Desktop desktop = Desktop.getDesktop(); 
URI helpURI = /* some URI from the new dir's index.html */ 
desktop.browse(helpURI); 
+0

HMM,可以是http://stackoverflow.com/questions/5377104/how-to-extract-a-folder-from-jar的副本 – 2011-06-07 16:50:39

回答

1

Apache的org.apache.commons.io.FileUtils能为你做的

创建目录使用File.mkdir();

转换网址与org.apache.commons.io.FileUtils.toFile(URL)

使用org.apache.commons.io.FileUtils.copyFile()复制到文件。

+1

不是他的目录不能像文件一样处理,因为它在JAR里面。 – 2011-06-07 17:40:57

+0

我的错误,我认为他有'战争'中的资源,所以他们已经被部署者提取。 – 2011-06-07 18:15:27

+0

Mooie naam,btw = D – 2011-06-07 19:14:01

0

您可以使用命令jar tf [here your jarfile]。这将以相对于jarfile(1行= 1个文件)的完整路径列出JAR存档的内容。检查行是否从要提取的目录的路径开始,并使用Class.getResourceAsStream(URL)作为匹配行并将它们提取到临时文件夹。

这里是jar tf输出例如:

 
META-INF/MANIFEST.MF 
TicTacToe.class 
audio/ 
audio/beep.au 
audio/ding.au 
audio/return.au 
audio/yahoo1.au 
audio/yahoo2.au 
images/ 
images/cross.gif 
images/not.gif 
相关问题