简体   繁体   English

将图像从JAR文件复制到外部文件夹

[英]Copying images from JAR file to a folder outside

My file structure: 我的文件结构:

This is how it looks using netbeans project: 使用netbeans项目的外观如下:

-src
    -images
        -*.jpg
    -stock
        -*.java
-images (exact copy of -images) 

and here is my jar 这是我的罐子

-jar
    -images
        -*.jpg
    -stock
        -*.java
-images (folder is created but files don't get copied) 

My files imagesCopy is the one that I create and ImagesOrg is the one inside .jar / src 我的文件imagesCopy是我创建的文件,而ImagesOrg是.jar / src中的文件

 File imagesCopy = new File("images");
 File imagesOrg = new File(URLDecoder.decode(getClass().getResource("/images").getPath()));

 if (!imagesCopy.exists()) {
            imagesCopy.mkdir();
                for(final File child : imagesOrg.listFiles()) {
                    try{
                        Files.copy(child.toPath(), Paths.get(imagesCopy.getAbsolutePath()+"/"+child.getName()), REPLACE_EXISTING);
                    }catch(Exception e){
                        System.out.println(e);
                    }
                }
        }

The problem definitely lies with: 问题肯定在于:

File imagesOrg = new File(URLDecoder.decode(getClass().getResource("/images").getPath()));

When compiling it it gives me, which is the proper directory 编译时会给我,这是正确的目录

D:\Code\build\classes\images 

which is the right directory, but when using this program from jar file I get: 这是正确的目录,但是当从jar文件使用此程序时,我得到:

D:\Code\dist\file:\D:\Code\dist\egz.jar!\images

and I assume that it should just be: 我认为应该只是:

D:\Code\dist\egz.jar!\images

without that first part 没有第一部分

Probably the simplest way to do it is like this: 大概最简单的方法是这样的:

public static void main(String[] args) throws URISyntaxException, IOException {
    File imagesCopy = new File("C:\\Users\\<YOURNAMEHERE>\\images");

    URI uri = ImageCopy.class.getResource("/images").toURI();
    if (!uri.toString().startsWith("file:")) {
        Map<String, String> env = new HashMap<>();
        env.put("create", "true");
        FileSystems.newFileSystem(uri, env);
    }
    Path imagesOrg = Paths.get(uri);
    System.out.println(imagesOrg);

    if (!imagesCopy.exists()) {
        imagesCopy.mkdir();
        try(DirectoryStream<Path> paths = Files.newDirectoryStream(imagesOrg)) {
            for (final Path child : paths) {
                System.out.println(child);
                try {
                    String targetPath = imagesCopy.getAbsolutePath() + File.separator + child.getFileName().toString();
                    System.out.println(targetPath);
                    Files.copy(child, Paths.get(targetPath), StandardCopyOption.REPLACE_EXISTING);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

It's not super-pretty, but it works. 它不是很漂亮,但是可以。 Might need to fiddle with the code if you have nested directories. 如果您有嵌套目录,则可能需要摆弄代码。

Note that you must create the FileSystem before accessing it (as per the Oracle Docs ). 请注意,您必须在访问文件系统之前创建文件系统 (按照Oracle Docs的要求 )。 I don't know why this is required, but there we go. 我不知道为什么要这样做,但是我们走了。

I've tested this and it will copy files from inside your JAR to wherever you would like. 我已经对此进行了测试,它将把文件从JAR内部复制到您想要的任何位置。

Here is a simple code to do it. 这是一个简单的代码。 You can adapt as you need. 您可以根据需要进行调整。

package br.com.jjcampos.main;

//imports here

public class CopyImage {

    private static ClassLoader loader = CopyImage.class.getClassLoader();

    public static void main(String[] args) throws IOException {
        InputStream stream = loader.getResourceAsStream("br/com/jjcampos/images/test.jpg");

        OutputStream outputStream = 
                new FileOutputStream(new File("c:/temp/newImage.jpg"));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = stream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
        outputStream.close();
    }
}

Understand that you can't copy a source from a stream (your jar) as a list of files. 了解您不能将流(您的jar)中的源复制为文件列表。 Unless you want to unpack it first. 除非您要先打开包装。 My suggestion is you to add a txt file with the list of your images then you read this file and use suggested code to copy each one. 我的建议是您添加一个带有图像列表的txt文件,然后阅读此文件并使用建议的代码复制每个文件。

Something like this: 像这样:

public class CopyImage {

    private static ClassLoader loader = CopyImage.class.getClassLoader();

    public static void main(String[] args) throws IOException {
        copyImages("c:/temp/");
    }


    public static void copyImages(String pathDestiny) throws IOException{
        InputStream listOfFiles = loader
           .getResourceAsStream("br/com/jjcampos/images/listImages.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(listOfFiles));
        String line;
        while ( (line = reader.readLine())!=null ){
            InputStream stream = loader.getResourceAsStream("br/com/jjcampos/images/" 
                                                               + line);
            OutputStream outputStream = 
                    new FileOutputStream(new File(pathDestiny + line));
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = stream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
            outputStream.close();
        }
    }
}

And your listImages.txt with 和你的listImages.txt

test.jpg

And you should decide if you put the full path on the text file or not to use in your code. 然后,您应该确定是否将完整路径放在文本文件上,或者不将其用于代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM