从 jar 文件复制目录 [英] Copy directory from a jar file
问题描述
我最近开发了一个应用程序并创建了 jar 文件.
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.
我的代码是这样的:
// 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:
使用Streams解决类似问题在其他课程上,但它不起作用与目录.代码类似于http://www.exampledepot.com/egs/java.io/CopyFile.html
使用 new File(getClass().getResource("template").toUri())
在写这篇文章的时候,我在想,而不是在资源路径中有一个包含 zip 文件的模板目录.这样做我可以将文件作为 inputStream 获取并将其解压缩到我需要的位置.但我不确定这是否是正确的方法.
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.
推荐答案
我认为您使用 zip 文件的方法是有道理的.大概你会做一个 getResourceAsStream
来获取 zip 的内部结构,它在逻辑上看起来像一个目录树.
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.
骨架方法:
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
}
这篇关于从 jar 文件复制目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!