从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解决类似的问题
on other课程,但它不起作用
与dirs。代码类似于
http://www.exampledepot.com/egs /java.io/CopyFile.html
使用新文件创建文件模板(getClass()。getResource( template)。toUri())
在写这篇文章时我正在考虑而不是在具有zip文件的资源路径中具有模板dir。这样做我可以将文件作为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屋!