获取目录中的所有图像 - 在jar文件中 [英] Get all images within directory - within jar file

查看:95
本文介绍了获取目录中的所有图像 - 在jar文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个包含238张图像的文件夹。我希望能够找到目录中的所有图像。

I have a folder inside my project that has 238 images. I want to be able to find all images within the directory.

我目前正在访问所有这些图像:

I'm currently accessing all these images like this:

File directory = new File(FileNameGuesser.class.getResource(DIRECTORY).getPath());
for (File file : directory.listFiles()) {
    // filter, process, etc.
}

这在Eclipse中运行良好。但是,当我导出到jar文件时, FileNameGuesser.class.getResource(DIRECTORY)返回 C:\ Users \ ... \ file.jar!\org \ ... (因为它是压缩的,我假设)并且方法中断了。

This works fine within Eclipse. However, when I export to a jar file, FileNameGuesser.class.getResource(DIRECTORY) returns C:\Users\...\file.jar!\org\... (because it's zipped, I assume) and the method breaks.

我怎么能实现这个目标?

How can I achieve this?

编辑:如果可能的话,我想找到一个既适用于Eclipse 又适用于在已部署的jar中。

EDIT: If possible, I'd like to find a solution that works both in Eclipse and in the jar that's deployed.

推荐答案

这不是真的可能在任何好的和干净的方式。

This isn't really possible in any nice and clean way.

能够做 .getClass()。getResources(/ pictures / * .jpg)(或其他东西),但我们不能。

It would be nice to be able to do .getClass().getResources("/pictures/*.jpg") (or something), but we can't.

你能做的最好就是作弊。如果您知道存储图像的jar文件的名称,则可以使用 JarFile ZipFile API获取列表:

The best you can do is cheat a little. If you know the name of the jar file where the images are stored, you can use either the JarFile or ZipFile APIs to get a listing:

ZipFile zipFile = null;
try {

    zipFile = new ZipFile(new File("...")); // Path to your Jar
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {

        ZipEntry entry = entries.nextElement();

        // Here, I've basically looked for the "pictures" folder in the Zip
        // You will need to provide the appropriate value
        if (!entry.isDirectory() && entry.getName().startsWith("pictures")) {

            // Basically, from here you should have the full name of the
            // image.  You should be able to then construct a resource path
            // to the image to load it...

            // URL url = getClass().getResource("/" + entry.getName());
            System.out.println(entry.getName());

        }

    }

} catch (Exception exp) {

    exp.printStackTrace();

} finally {


    try {
        zipFile.close();
    } catch (Exception e) {
    }

}

如果你事先不知道他们的名字,那么更好的解决办法就是不要将这些图像嵌入你的罐子里。

A better solution would be not to embedded these images in your jar, if you don't know their names in advance.

嵌入式资源真的应该知道事先通过名称提交给您的申请。

Embedded resources really should be known by name to your application in advance.

正如AndrewThompson所建议的那样,您可以生成资源列表,将其添加到您的Jar文件中。在运行时,您将加载此文件并可以访问所有资源。

As suggested by AndrewThompson, you could generate a list of resources, add this to your Jar file. At runtime, you would load this file and have access to all the resources.

这篇关于获取目录中的所有图像 - 在jar文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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