开发者

Extract a particular folder from a jar and copy it to a desired destination on my system

开发者 https://www.devze.com 2023-03-19 04:49 出处:网络
I need to extract the resource folder from inside a jar to a desired location in my system. I want to do it by calling a function in a class, which is in the same jar.

I need to extract the resource folder from inside a jar to a desired location in my system. I want to do it by calling a function in a class, which is in the same jar.

I do开发者_开发百科n't want to copy one file at a time. Can you please suggest me a way in which I can copy the entire folder?

I initially thought of compressing them into a zip, and copying it elsewhere, and extracting it. How will this work? Is there a more efficient way to do this?

Thanks in advance.


If you are going to do this using java API I know only one way: you have to use JarInputStream or ZipInputStream, iterate over Zip entries, detect which entries belong to the folder and extract them, i.e. read from zip and write to disk. There is no other "magical" way.

But if you want you can probably use some kind of higher level API. Check VFS from Jakarta: http://commons.apache.org/vfs/

It provides API that probably does what you need.


You could use Runtime.exec api to execute something similar to the following :

jar xf <your_jar_file_name> <path_to_directory_to_be_extracted>

In this way, you do not have to create specialized class to handle Jar files and you can focus on solving actual problem at hand.

Note : this is restricted to JDK may not work on JRE.

0

精彩评论

暂无评论...
验证码 换一张
取 消