2011-06-07 123 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 
相關問題