如何从JAR中提取文件夹 [英] How to extract a folder from JAR
问题描述
我需要复制一个文件夹,在运行时打包在Jar中。我想通过调用同一个文件夹中包含的函数来实现。
I need to copy a folder, packed in a Jar on runtime. I want to do it by calling a function in a class which is also contained in the same folder.
我尝试过使用 getSystemResource
:
URL sourceDirUrl = ClassLoader.getSystemResource("sourceDirName");
File sourceDir = new File(sourceDirUrl.toURI());
但它不起作用。
我可能必须递归使用 getResourceAsStream
函数。是否有更优雅/更直接的方式来做到这一点?
but it doesn't work.
I probably have to use getResourceAsStream
function recursively. Is there a more elegant/strait-forward way to do this?
如果我必须递归地执行:
1.我不想指定硬编码的文件,我想动态地执行它
2.我不想创建单独的存档。我希望这个资源与处理它的类在同一个Jar中
In case I have to do it recursively: 1. I don't want to specify the files hard-coded, I want to do it dynamically 2. I don't want to create a separate archive. I want this resource to be in the same Jar as the classes dealing with it
谢谢
我最终了做Koziołek建议如下。虽然我希望有一个更优雅的解决方案,但它看起来像它一样好。
I ended up doing what Koziołek suggested below. Although I was hoping for a more elegant solution, but it looks like this is as good as it gets.
推荐答案
Jar是简单的ZIP文件。您可以使用java.util.zip。*包来解压缩文件。
Jar is simple ZIP file. You can use java.util.zip.* package to decompress files.
这篇关于如何从JAR中提取文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!