如何将资源文件夹从 jar 复制到程序文件中

How to copy resources folder out of jar into program files

我花了好几个小时寻找答案,但我就是想不通,我正在尝试复制我的资源文件夹,其中包含我正在开发的游戏的所有图像和数据文件运行ning jar 并放入 E:/Program Files/mtd/ 当我从 eclipse 中 运行 它工作正常,但是当我导出 jar 并尝试它时,我得到 NoSuchFileException

`JAR
Installing...
file:///C:/Users/Cam/Desktop/mtd.jar/resources to file:///E:/Program%20Files/mtd
/resources
java.nio.file.NoSuchFileException: C:\Users\Cam\Desktop\mtd.jar\resources
        at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
        at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
        at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
        at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(Unknown Sou
rce)
        at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(Unknown Sou
rce)
        at sun.nio.fs.WindowsFileSystemProvider.readAttributes(Unknown Source)
        at java.nio.file.Files.readAttributes(Unknown Source)
        at java.nio.file.FileTreeWalker.walk(Unknown Source)
        at java.nio.file.FileTreeWalker.walk(Unknown Source)
        at java.nio.file.Files.walkFileTree(Unknown Source)
        at java.nio.file.Files.walkFileTree(Unknown Source)
        at me.Zacx.mtd.main.Game.<init>(Game.java:94)
        at me.Zacx.mtd.main.Game.main(Game.java:301)`

这是我使用的代码:

    if (!pfFolder.exists()) {
    pfFolder.mkdir();
    try {

        URL url = getClass().getResource("/resources/");
        URI uri = null;

        if (url.getProtocol().equals("jar")) {
            System.out.println("JAR");
            JarURLConnection connect = (JarURLConnection) url.openConnection();
            uri = new URI(connect.getJarFileURL().toURI().toString() + "/resources/");

        } else if (url.getProtocol().equals("file")) {
            System.out.println("FILE");
            uri = url.toURI();
        }

        final Path src = Paths.get(uri);
        final Path tar = Paths.get(System.getenv("ProgramFiles") + "/mtd/resources/");

        System.out.println("Installing...");
        System.out.println(src.toUri() + " to " + tar.toUri());

        Files.walkFileTree(src, new SimpleFileVisitor<Path>() {
            public FileVisitResult visitFile( Path file, BasicFileAttributes attrs ) throws IOException {
                return copy(file);
            }
            public FileVisitResult preVisitDirectory( Path dir, BasicFileAttributes attrs ) throws IOException {
                return copy(dir);
            }
            private FileVisitResult copy( Path fileOrDir ) throws IOException {
                System.out.println("Copying " + fileOrDir.toUri() + " to " + tar.resolve( src.relativize( fileOrDir ) ).toUri());
                Files.copy( fileOrDir, tar.resolve( src.relativize( fileOrDir ) ) );
                return FileVisitResult.CONTINUE;
            }
        });
        System.out.println("Done!");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

这里的问题是不同的文件系统。 C:/Users/Cam/Desktop/mtd.jarWindowsFileSystem 中的一个 File。由于它是文件而不是目录,因此您无法访问文件中的子目录; C:/Users/Cam/Desktop/mtd.jar/resources 只有在 mtd.jar 实际上是 目录 而不是 文件 时才是有效的 Path。 =39=]

为了访问不同文件系统上的内容,您必须使用从该文件系统的根开始的路径。例如,如果您在 D:\dir1\dir2\file 中有一个文件,则无法使用以 C:\ 开头的路径访问它(无法承受符号链接);您必须使用从该文件系统的根目录开始的路径 D:\.

jar 文件只是一个文件。它可以位于文件系统中的任何位置,并且可以像任何常规文件一样移动、复制或删除。但是,它本身包含自己的文件系统。没有 windows 路径可用于引用 jar 文件系统中的任何文件,就像没有以 C:\ 开头的路径可以引用 D:\ 文件系统中的任何文件一样。

为了访问 jar 的内容,您必须以 ZipFileSystem.

打开 jar
// Autoclose the file system at end of try { ... } block.
try(FileSystem zip_fs = FileSystems.newFileSystem(pathToZipFile, null)) {
}

一旦你有了 zip_fs,你就可以使用 zip_fs.getPath("/path/in/zip"); 得到一个 Path 到其中的一个文件。这个 Path 对象实际上是一个 ZipFileSystemProvider 路径对象,而不是一个 WindowsFileSystemProvider 路径对象,但除此之外它是一个可以打开、读取等的 Path 对象。 ,至少在 ZipFileSystem 关闭之前。最大的区别是 path.getFileSystem() 将 return ZipFileSystem,并且 resolve()relativize() 不能使用 getFileSystem() [=64= 的路径对象]s 不同的文件系统。

当您的项目 运行 来自 Eclipse 时,所有资源都在 WindowsFileSystem 中,因此遍历文件系统树并复制资源是直截了当的。当您的项目 运行 来自 jar 时,资源不在默认文件系统中。

这是一个Java class,它将资源复制到安装目录。它将在 Eclipse 中工作(所有资源作为单独的文件),以及当应用程序被打包到 jar 中时。

public class Installer extends SimpleFileVisitor<Path> {

    public static void installResources(Path dst, Class<?> cls, String root) throws URISyntaxException, IOException {
        URL location = cls.getProtectionDomain().getCodeSource().getLocation();
        if (location.getProtocol().equals("file")) {
            Path path = Paths.get(location.toURI());
            if (location.getPath().endsWith(".jar")) {
                try (FileSystem fs = FileSystems.newFileSystem(path, null)) {
                    installResources(dst, fs.getPath("/" + root));
                }
            } else {
                installResources(dst, path.resolve(root));
            }
        } else {
            throw new IllegalArgumentException("Not supported: " + location);
        }
    }

    private static void installResources(Path dst, Path src) throws IOException {
        Files.walkFileTree(src, new Installer(dst, src));
    }

    private final Path target, source;

    private Installer(Path dst, Path src) {
        target = dst;
        source = src;
    }

    private Path resolve(Path path) {
        return target.resolve(source.relativize(path).toString());
    }

    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        Path dst = resolve(dir);
        Files.createDirectories(dst);
        return super.preVisitDirectory(dir, attrs);
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Path dst = resolve(file);
        Files.copy(Files.newInputStream(file), dst, StandardCopyOption.REPLACE_EXISTING);
        return super.visitFile(file, attrs);
    }
}

称为:

    Path dst = Paths.get("C:\Program Files\mtd");
    Installer.installResources(dst, Game.class, "resources");

这比我想象的要难,但这里是如何做到的。

这里是我的复制方法参考https://examples.javacodegeeks.com/core-java/io/file/4-ways-to-copy-file-in-java/

public void copyFile(String inputPath, String outputPath ) throws IOException
{

    InputStream inputStream = null;

    OutputStream outputStream = null;
    try {

        inputStream =  getClass().getResourceAsStream(inputPath);
        outputStream = new FileOutputStream(outputPath);

        byte[] buf = new byte[1024];

        int bytesRead;

        while ((bytesRead = inputStream.read(buf)) > 0) {

            outputStream.write(buf, 0, bytesRead);

       }

    }
    finally {


        inputStream.close();

        outputStream.close();

    }

请注意此图中Jar文件的工程结构Project structure

现在我需要读取 Jar 文件。这是此解决方案 How can I get a resource "Folder" from inside my jar File? 的变体。这两种方法共同作用以产生结果。我已经测试过这个并且它有效。

public class 主 {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    final String pathPartOne = "test/com";
    final String pathPartTwo = "/MyResources";
    String pathName = "C:\Users\Jonathan\Desktop\test.jar";

    JarTest test = new JarTest();

    final File jarFile = new File(pathName);

    if(jarFile.isFile()) {  // Run with JAR file
        final JarFile jar = new JarFile(jarFile);
        final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
        while(entries.hasMoreElements()) {
            final String name = entries.nextElement().getName();

            if (name.startsWith(pathPartOne+pathPartTwo + "/")) { //filter according to the path
                if(name.contains("."))//has extension
                {
                    String relavtivePath = name.substring(pathPartOne.length()+1);
                    String fileName = name.substring(name.lastIndexOf('/')+1);
                    System.out.println(relavtivePath);
                    System.out.println(fileName);
                    test.copyFile(relavtivePath, "C:\Users\Jonathan\Desktop\" + fileName);
                }

            }
        }
        jar.close();
    } 


}

}

希望对您有所帮助。

我终于找到了答案 我不想打出冗长的解释,但对于任何正在寻找解决方案的人来说,这里是

 `  
              //on startup
               installDir("");
                for (int i = 0; i < toInstall.size(); i++) {
                    File f = toInstall.get(i);
                    String deepPath = f.getPath().replace(f.getPath().substring(0, f.getPath().lastIndexOf("resources") + "resources".length() + 1), "");
                    System.out.println(deepPath);
                    System.out.println("INSTALLING: " + deepPath);
                    installDir(deepPath);
                    System.out.println("INDEX: " + i);
                }

public void installDir(String path) {
            System.out.println(path);
            final URL url = getClass().getResource("/resources/" + path);
            if (url != null) {
                try {
                    final File apps = new File(url.toURI());
                    for (File app : apps.listFiles()) {
                        System.out.println(app);
                            System.out.println("copying..." + app.getPath() + " to " + pfFolder.getPath());
                            String deepPath = app.getPath().replace(app.getPath().substring(0, app.getPath().lastIndexOf("resources") + "resources".length() + 1), "");
                            System.out.println(deepPath);

                        try {

                                File f = new File(resources.getPath() + "/" + deepPath);
                                if (getExtention(app) != null) {
                                FileOutputStream resourceOS = new FileOutputStream(f);
                                byte[] byteArray = new byte[1024];
                                int i;
                                InputStream classIS = getClass().getClassLoader().getResourceAsStream("resources/" + deepPath);
                        //While the input stream has bytes
                                while ((i = classIS.read(byteArray)) > 0) 
                                {
                        //Write the bytes to the output stream
                                    resourceOS.write(byteArray, 0, i);
                                }
                        //Close streams to prevent errors
                                classIS.close();
                                resourceOS.close();
                                } else {
                                    System.out.println("new dir: " + f.getPath() + " (" + toInstall.size() + ")");
                                    f.mkdir();
                                    toInstall.add(f);
                                    System.out.println(toInstall.size());
                                }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (URISyntaxException ex) {
                    // never happens
                }
            }

        }`