如何在运行时和 dev-env 中从 jar 中复制文件夹的资源?
How to copy folder's out of resources from jar both in runtime and dev-env?
我目前正在制作一款具有预制关卡的游戏,目前我将它们存储在资源中。我想要一个关于如何在生产和开发环境中从 jar 中提取文件夹的解决方案。
我尝试使用下面给定的方法复制文件夹并将 src 作为 File defaultWorld = new File(GameData.class.getClassLoader().getResource("worlds/").getFile());
目的地为 private static File worldsDir = new File("run/worlds");
public static void copyFolder(File src, File dest) {
try {
if (src.isDirectory()) {
if (!dest.exists()) {
dest.mkdir();
}
String[] files = src.list();
for (String file : files) {
copyFolder(new File(src, file), new File(dest, file));
}
} else {
try (InputStream in = new FileInputStream(src)) {
try (OutputStream out = new FileOutputStream(dest)) {
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
我希望上述方法适用于开发环境和生产环境,但在打开文件输出流时抛出 FileNotFoundException
。
您不能列出 jar 中的资源。
您想到的任何解决方法都不可靠。
- 从不调用URL的
getFile()
方法。它不是return一个有效的文件名;它只是 return 的路径和 URL 的查询部分,任何 percent-escapes 都完好无损。此外,jar 条目不是 file:
URL,因此资源路径在引用 jar 条目时永远不可能是有效的文件名。
- 在 jar 文件中列出内容的唯一方法是遍历所有 jar 条目,但您甚至不能保证可以访问您的 jar,因为不能保证 ClassLoaders 是 URLClassLoaders , 一般不保证使用
jar:
URLs.
- 你甚至不能依赖 MyApplication.class.getProtectionDomain().getCodeSource(),因为 getCodeSource() can return null.
如果你想从你的 jar 中复制多个文件,这里有一些可靠的方法:
- 硬编码您计划复制的资源列表。这是您的应用程序,因此您知道要放入 jar 中的文件。
- 在您的 jar 中保留一个文本文件,其中包含要复制的资源路径列表。
- 将您的资源存储在嵌入在您的 jar 中的单个 zip 存档中,并使用 ZipInputStream 包装自己将其解压缩 MyApplication.class.getResourceAsStream.
我目前正在制作一款具有预制关卡的游戏,目前我将它们存储在资源中。我想要一个关于如何在生产和开发环境中从 jar 中提取文件夹的解决方案。
我尝试使用下面给定的方法复制文件夹并将 src 作为 File defaultWorld = new File(GameData.class.getClassLoader().getResource("worlds/").getFile());
目的地为 private static File worldsDir = new File("run/worlds");
public static void copyFolder(File src, File dest) {
try {
if (src.isDirectory()) {
if (!dest.exists()) {
dest.mkdir();
}
String[] files = src.list();
for (String file : files) {
copyFolder(new File(src, file), new File(dest, file));
}
} else {
try (InputStream in = new FileInputStream(src)) {
try (OutputStream out = new FileOutputStream(dest)) {
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
我希望上述方法适用于开发环境和生产环境,但在打开文件输出流时抛出 FileNotFoundException
。
您不能列出 jar 中的资源。
您想到的任何解决方法都不可靠。
- 从不调用URL的
getFile()
方法。它不是return一个有效的文件名;它只是 return 的路径和 URL 的查询部分,任何 percent-escapes 都完好无损。此外,jar 条目不是file:
URL,因此资源路径在引用 jar 条目时永远不可能是有效的文件名。 - 在 jar 文件中列出内容的唯一方法是遍历所有 jar 条目,但您甚至不能保证可以访问您的 jar,因为不能保证 ClassLoaders 是 URLClassLoaders , 一般不保证使用
jar:
URLs. - 你甚至不能依赖 MyApplication.class.getProtectionDomain().getCodeSource(),因为 getCodeSource() can return null.
如果你想从你的 jar 中复制多个文件,这里有一些可靠的方法:
- 硬编码您计划复制的资源列表。这是您的应用程序,因此您知道要放入 jar 中的文件。
- 在您的 jar 中保留一个文本文件,其中包含要复制的资源路径列表。
- 将您的资源存储在嵌入在您的 jar 中的单个 zip 存档中,并使用 ZipInputStream 包装自己将其解压缩 MyApplication.class.getResourceAsStream.