如何在运行时和dev-env中从jar复制资源不足的文件夹? [英] How to copy folder's out of resources from jar both in runtime and dev-env?

查看:94
本文介绍了如何在运行时和dev-env中从jar复制资源不足的文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作一款具有预先制作的关卡的游戏,并且目前将它们存储在资源中.我想要一个解决方案,以解决在生产和开发环境中如何从jar中提取文件夹的问题.

I am currently making a game which have levels which are pre-made and i currently store them in resources. I want a solution to how i can extract a folder out of a jar in production and development environment.

我尝试使用下面的给定方法复制文件夹,并将src传递为File defaultWorld = new File(GameData.class.getClassLoader().getResource("worlds/").getFile()); 目的地为private static File worldsDir = new File("run/worlds");

I have tried copying the folder by using the given method below and pass the src as File defaultWorld = new File(GameData.class.getClassLoader().getResource("worlds/").getFile()); and destination as 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.

I expected the above method to work on both dev and production env but it throws FileNotFoundException when opening file output stream.

推荐答案

无法列出jar中的资源.

You cannot list resources in a jar.

您所考虑的任何解决方法都不可靠.

Any workarounds you’re thinking of, are unreliable.

  • 从不调用URL的getFile()方法.它返回有效的文件名;它只返回URL的路径和查询部分,并保留所有百分号转义.此外,jar条目不是file: URL,因此,当资源路径引用jar条目时,它永远不能是有效的文件名.
  • 列出jar文件中所有内容的唯一方法是遍历所有jar条目,但是甚至不能保证可以访问jar,因为不能保证ClassLoaders是URLClassLoaders,而且通常也不能保证使用jar: URL.
  • 您甚至不能依靠MyApplication.class.getProtectionDomain().getCodeSource(),因为
  • Never call the getFile() method of URL. It does not return a valid file name; it just returns the path and query portions of the URL, with any percent-escapes intact. Furthermore, jar entries are not file: URLs, so a resource path can never be a valid file name when it refers to a jar entry.
  • The only way to list things in a jar file is by iterating through all jar entries, but you aren’t even guaranteed to have access to your jar, because ClassLoaders are not guaranteed to be URLClassLoaders, and in general are not guaranteed to use jar: URLs.
  • You can’t even rely on MyApplication.class.getProtectionDomain().getCodeSource(), because getCodeSource() can return null.

如果您要从jar中复制多个文件,可以使用以下几种可靠的方法:

If you want to copy multiple files from your jar, here are some reliable ways to do it:

  • Hard code the list of resources you plan to copy. It’s your application, so you know what files you’re putting in the jar.
  • Keep a single text file in your jar which contains a list of resource paths to copy.
  • Store your resources in a single zip archive which is embedded in your jar, and extract it yourself with a ZipInputStream which wraps MyApplication.class.getResourceAsStream.

这篇关于如何在运行时和dev-env中从jar复制资源不足的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆