Reputation: 40036
I am trying to extract certain types of file from my classpath and put to a temp directory, and I want the resources to be extracted according to its "relative path to classpath".
I am able to use Spring's PathMatchingResourcePatternResolver to retrieve list of matching resources. For example, I am scanning for classpath*:/xyz/**/*.bar
from the classpath, assume in classpath I have a /xyz/some/dir/foo.bar
, and I want to extract this resource to MY_TEMP_DIR/some/dir/foo.bar
. Is there anyway to get the relative path of this resource to classpath so I can construct the output location accordingly?
PathMatchingResourcePatternResolver is only returning array of Resource, for which I can only retrieve the URL of the resources, but I want to know also what's the "root" directory of this resources. The Resource returned can be in various "format", like jar:file:/some/path/somejar.jar!/xyz/some/dir/foo.bar
, file:/some/output/directory/xyz/some/dir/foo.bar
etc.
Can someone give me hints on how to achieve the extracting or classpath-root-relative-path lookup of a resource?
Thanks
Edit:
I am not sure if it is better if I put my question in another way:
Is there any way to know what's the "relative path to classpath" of a resource?
Just use my previous examples, if I scan classpath*:/xyz/**/*.bar
, and I get a resource file:/some/output/directory/xyz/some/dir/foo.bar
, is there any way I can get the "relative path" of that resource (/xyz/some/dir/foo.bar
) ?
Upvotes: 3
Views: 2151
Reputation: 51441
You you use the PathMatchingResourcePatternResolver.getResources(String pattern)
to get a list of resources matching the pattern, it should be trivial to parse the URL returned by Resource.getURL().
It should look something like jar:file://path/to/jar.jar!/path/to/your/resource/foo.bar
Upvotes: 1