I have recently developed an application and created the jar file.
One of my classes creates an output directory, populating it with files from its resource.
My code is something like this:
// Copy files from dir "template" in this class resource to output.
private void createOutput(File output) throws IOException {
File template = new File(FileHelper.URL2Path(getClass().getResource("template")));
FileHelper.copyDirectory(template, output);
}
Unfortunately this doesn't work.
I tried the following without luck:
Using Streams to solve similar stuff on other classes but it doesn't work with dirs. Code was similar to http://www.exampledepot.com/egs/java.io/CopyFile.html
Creating the File template with
new File(getClass().getResource("template").toUri())
While writing this I was thinking about instead of having a template dir in the resource path having a zip file of it. Doing it this way I could get the file as an inputStream and unzip it where I need to. But I am not sure if it's the correct way.
I think your approach of using a zip file makes sense. Presumably you'll do a getResourceAsStream
to get at the internals of the zip, which will logically look like a directory tree.
A skeleton approach:
InputStream is = getClass().getResourceAsStream("my_embedded_file.zip");
ZipInputStream zis = new ZipInputStream(is);
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
// do something with the entry - for example, extract the data
}
Thanks for the solution! For others, the following doesn't make use of the auxiliary classes (except for StringUtils)
/I added extra information for this solution, check the end of the code, Zegor V/
public class FileUtils {
public static boolean copyFile(final File toCopy, final File destFile) {
try {
return FileUtils.copyStream(new FileInputStream(toCopy),
new FileOutputStream(destFile));
} catch (final FileNotFoundException e) {
e.printStackTrace();
}
return false;
}
private static boolean copyFilesRecusively(final File toCopy,
final File destDir) {
assert destDir.isDirectory();
if (!toCopy.isDirectory()) {
return FileUtils.copyFile(toCopy, new File(destDir, toCopy.getName()));
} else {
final File newDestDir = new File(destDir, toCopy.getName());
if (!newDestDir.exists() && !newDestDir.mkdir()) {
return false;
}
for (final File child : toCopy.listFiles()) {
if (!FileUtils.copyFilesRecusively(child, newDestDir)) {
return false;
}
}
}
return true;
}
public static boolean copyJarResourcesRecursively(final File destDir,
final JarURLConnection jarConnection) throws IOException {
final JarFile jarFile = jarConnection.getJarFile();
for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) {
final JarEntry entry = e.nextElement();
if (entry.getName().startsWith(jarConnection.getEntryName())) {
final String filename = StringUtils.removeStart(entry.getName(), //
jarConnection.getEntryName());
final File f = new File(destDir, filename);
if (!entry.isDirectory()) {
final InputStream entryInputStream = jarFile.getInputStream(entry);
if(!FileUtils.copyStream(entryInputStream, f)){
return false;
}
entryInputStream.close();
} else {
if (!FileUtils.ensureDirectoryExists(f)) {
throw new IOException("Could not create directory: "
+ f.getAbsolutePath());
}
}
}
}
return true;
}
public static boolean copyResourcesRecursively( //
final URL originUrl, final File destination) {
try {
final URLConnection urlConnection = originUrl.openConnection();
if (urlConnection instanceof JarURLConnection) {
return FileUtils.copyJarResourcesRecursively(destination,
(JarURLConnection) urlConnection);
} else {
return FileUtils.copyFilesRecusively(new File(originUrl.getPath()),
destination);
}
} catch (final IOException e) {
e.printStackTrace();
}
return false;
}
private static boolean copyStream(final InputStream is, final File f) {
try {
return FileUtils.copyStream(is, new FileOutputStream(f));
} catch (final FileNotFoundException e) {
e.printStackTrace();
}
return false;
}
private static boolean copyStream(final InputStream is, final OutputStream os) {
try {
final byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
is.close();
os.close();
return true;
} catch (final IOException e) {
e.printStackTrace();
}
return false;
}
private static boolean ensureDirectoryExists(final File f) {
return f.exists() || f.mkdir();
}
}
It uses only one external library from the Apache Software Foundation, however the used functions are only :
public static String removeStart(String str, String remove) {
if (isEmpty(str) || isEmpty(remove)) {
return str;
}
if (str.startsWith(remove)){
return str.substring(remove.length());
}
return str;
}
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
My knowledge is limited on Apache licence, but you can use this methods in your code without library. However, i am not responsible for licence issues, if there is.
Using Java7+ this can be achieved by creating FileSystem
and then using walkFileTree
to copy files recursively.
public void copyFromJar(String source, final Path target) throws URISyntaxException, IOException {
URI resource = getClass().getResource("").toURI();
FileSystem fileSystem = FileSystems.newFileSystem(
resource,
Collections.<String, String>emptyMap()
);
final Path jarPath = fileSystem.getPath(source);
Files.walkFileTree(jarPath, new SimpleFileVisitor<Path>() {
private Path currentTarget;
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
currentTarget = target.resolve(jarPath.relativize(dir).toString());
Files.createDirectories(currentTarget);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, target.resolve(jarPath.relativize(file).toString()), StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
}
The method can be used like this:
copyFromJar("/path/to/the/template/in/jar", Paths.get("/tmp/from-jar"))
I hated the idea of using the ZIP file method posted earlier, so I came up with the following.
public void copyResourcesRecursively(URL originUrl, File destination) throws Exception {
URLConnection urlConnection = originUrl.openConnection();
if (urlConnection instanceof JarURLConnection) {
copyJarResourcesRecursively(destination, (JarURLConnection) urlConnection);
} else if (urlConnection instanceof FileURLConnection) {
FileUtils.copyFilesRecursively(new File(originUrl.getPath()), destination);
} else {
throw new Exception("URLConnection[" + urlConnection.getClass().getSimpleName() +
"] is not a recognized/implemented connection type.");
}
}
public void copyJarResourcesRecursively(File destination, JarURLConnection jarConnection ) throws IOException {
JarFile jarFile = jarConnection.getJarFile();
for (JarEntry entry : CollectionUtils.iterable(jarFile.entries())) {
if (entry.getName().startsWith(jarConnection.getEntryName())) {
String fileName = StringUtils.removeStart(entry.getName(), jarConnection.getEntryName());
if (!entry.isDirectory()) {
InputStream entryInputStream = null;
try {
entryInputStream = jarFile.getInputStream(entry);
FileUtils.copyStream(entryInputStream, new File(destination, fileName));
} finally {
FileUtils.safeClose(entryInputStream);
}
} else {
FileUtils.ensureDirectoryExists(new File(destination, fileName));
}
}
}
}
Example Useage (copies all files from the classpath resource "config" to "${homeDirectory}/config":
File configHome = new File(homeDirectory, "config/");
//noinspection ResultOfMethodCallIgnored
configHome.mkdirs();
copyResourcesRecursively(super.getClass().getResource("/config"), configHome);
This should work both for copying from both flat files as well as Jar files.
Note: The code above uses some custom utility classes (FileUtils, CollectionUtils) as well as some from Apache commons-lang (StringUtils), but the functions should be named fairly obviously.
The answer of lpiepiora, is correct! But there is a minor issue, The source, should be a jar Url. When the source path is path to a file system, then the above code will not work proper. To solve this problem, you should use the ReferencePath, the code, you can get from the following link: Read from file system via FileSystem object The new code of copyFromJar should like:
public class ResourcesUtils {
public static void copyFromJar(final String sourcePath, final Path target) throws URISyntaxException,
IOException {
final PathReference pathReference = PathReference.getPath(new URI(sourcePath));
final Path jarPath = pathReference.getPath();
Files.walkFileTree(jarPath, new SimpleFileVisitor<Path>() {
private Path currentTarget;
@Override
public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
currentTarget = target.resolve(jarPath.relativize(dir)
.toString());
Files.createDirectories(currentTarget);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
Files.copy(file, target.resolve(jarPath.relativize(file)
.toString()), StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
}
public static void main(final String[] args) throws MalformedURLException, URISyntaxException, IOException {
final String sourcePath = "jar:file:/c:/temp/example.jar!/src/main/resources";
ResourcesUtils.copyFromJar(sourcePath, Paths.get("c:/temp/resources"));
}
I'm not sure what FileHelper
is or does, but you will NOT be able to copy files (or directories) directly from JAR. Using InputStream as you've mentioned is the correct way (from either jar or zip):
InputStream is = getClass().getResourceAsStream("file_in_jar");
OutputStream os = new FileOutputStream("dest_file");
byte[] buffer = new byte[4096];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.close();
is.close();
You'll need to do the above (handling exceptions appropriately, of course) for each of your files. You may or may not be able (depending on your deployment configuration) to read jar file in question as JarFile (it may not be available as an actual file if deployed as part of non-expanded web app, for example). If you can read it, you should be able to iterate through list of JarEntry instances and thus reconstitute your directory structure; otherwise you may need to store it elsewhere (within text or xml resource, for example)
You may want to take a look at Commons IO library - it provides a lot of commonly used stream / file functionality including copying.
Here is a working version from the tess4j project:
/**
* This method will copy resources from the jar file of the current thread and extract it to the destination folder.
*
* @param jarConnection
* @param destDir
* @throws IOException
*/
public void copyJarResourceToFolder(JarURLConnection jarConnection, File destDir) {
try {
JarFile jarFile = jarConnection.getJarFile();
/**
* Iterate all entries in the jar file.
*/
for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) {
JarEntry jarEntry = e.nextElement();
String jarEntryName = jarEntry.getName();
String jarConnectionEntryName = jarConnection.getEntryName();
/**
* Extract files only if they match the path.
*/
if (jarEntryName.startsWith(jarConnectionEntryName)) {
String filename = jarEntryName.startsWith(jarConnectionEntryName) ? jarEntryName.substring(jarConnectionEntryName.length()) : jarEntryName;
File currentFile = new File(destDir, filename);
if (jarEntry.isDirectory()) {
currentFile.mkdirs();
} else {
InputStream is = jarFile.getInputStream(jarEntry);
OutputStream out = FileUtils.openOutputStream(currentFile);
IOUtils.copy(is, out);
is.close();
out.close();
}
}
}
} catch (IOException e) {
// TODO add logger
e.printStackTrace();
}
}
You could use the ClassLoader to obtain a stream to the resource. Once you have obtained an InputStream, you can read off, and write the contents of the stream, onto an OutputStream.
In your case, you'll need to create several OutputStream instances, one for each file that you want to copy over to the destination. This of course, requires that you know of the file names before hand.
For this task, it is preferred to use getResourceAsStream, rather than getResource or getResources().
I have faced the similair problem recently. I tried to extract folder from java resources. So I resolved this issue with Spring PathMatchingResourcePatternResolver.
This code gets all files and directories from the specified resource:
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
+ resourceFolder + "/**");
And this is the class that copy all files and directories from the resource to the disk path.
public class ResourceExtractor {
public static final Logger logger =
Logger.getLogger(ResourceExtractor.class);
public void extract(String resourceFolder, String destinationFolder){
try {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
+ resourceFolder + "/**");
URI inJarUri = new DefaultResourceLoader().getResource("classpath:" + resourceFolder).getURI();
for (Resource resource : resources){
String relativePath = resource
.getURI()
.getRawSchemeSpecificPart()
.replace(inJarUri.getRawSchemeSpecificPart(), "");
if (relativePath.isEmpty()){
continue;
}
if (relativePath.endsWith("/") || relativePath.endsWith("\\")) {
File dirFile = new File(destinationFolder + relativePath);
if (!dirFile.exists()) {
dirFile.mkdir();
}
}
else{
copyResourceToFilePath(resource, destinationFolder + relativePath);
}
}
}
catch (IOException e){
logger.debug("Extraction failed!", e );
}
}
private void copyResourceToFilePath(Resource resource, String filePath) throws IOException{
InputStream resourceInputStream = resource.getInputStream();
File file = new File(filePath);
if (!file.exists()) {
FileUtils.copyInputStreamToFile(resourceInputStream, file);
}
}
}
来源:https://stackoverflow.com/questions/1386809/copy-directory-from-a-jar-file