从 jar 中提取特定文件夹并将其复制到我的系统上所需的目的地
我需要将资源文件夹从 jar 中提取到系统中的所需位置。我想通过调用类中的函数来完成此操作,该类位于同一个 jar 中。
我不想一次复制一个文件。您能建议我一种复制整个文件夹的方法吗?
我最初想到将它们压缩成一个zip,然后将其复制到其他地方,然后解压。 这将如何运作?有没有更有效的方法来做到这一点?
提前致谢。
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 don'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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Runtime.exec api 执行类似于以下内容的操作:
这样,您不必创建专门的类来处理 Jar 文件,您可以专注于解决手头的实际问题。
注意:这仅限于 JDK,可能不适用于 JRE。
You could use
Runtime.exec
api to execute something similar to the following :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.
如果您打算使用 java API 来执行此操作,我只知道一种方法:您必须使用 JarInputStream 或 ZipInputStream,迭代 Zip 条目,检测哪些条目属于该文件夹并提取它们,即从 zip 读取并写入磁盘。没有其他“神奇”的方法了。
但如果你愿意,你可以使用某种更高级别的 API。从 Jakarta 检查 VFS: http://commons.apache.org/vfs/
它提供的 API 可能是做你需要的。
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.