2

I know how to create a temporary directory in Java, but is there an easy way to copy files in Java from the jar file to this directory?

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);
Jason S
  • 184,598
  • 164
  • 608
  • 970
  • hmm, may be a duplicate of http://stackoverflow.com/questions/5377104/how-to-extract-a-folder-from-jar – Jason S Jun 07 '11 at 16:50

2 Answers2

1

Apache's org.apache.commons.io.FileUtils can do that for you

To create directory use File.mkdir();

Convert URL to File with org.apache.commons.io.FileUtils.toFile(URL)

use org.apache.commons.io.FileUtils.copyFile() to copy.

Op De Cirkel
  • 28,647
  • 6
  • 40
  • 53
0

You can make use of the command jar tf [here your jarfile]. This will list the contents of the JAR Archive with their full path, relative to the jarfile (1 line = 1 file). Check if the line starts with the path of the directory you want to extract, and use Class.getResourceAsStream(URL) for the matching lines and extract them to your temporary folder.

Here is an example output of 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
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287