在Jar中读取文件(ZipInputStream等) [英] Reading Files Within a Jar(ZipInputStream, etc.)

查看:1151
本文介绍了在Jar中读取文件(ZipInputStream等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用java编写一个游戏,所有的工作或多或少都像你期望的那样编码,但是我碰到的一个问题是将图像和声音文件读入游戏。我写了一个在Eclipse中运行得很好的方法,但是当我尝试在可运行的jar(使用Eclipse导出)中测试它时,我的游戏就停留在加载屏幕上,因为它无法读取文件。我意识到存在的问题是您不能在Jar中创建File对象,您必须使用流。这可以工作,但图像/声音位于文件夹内,我不是很确定如何可以读取文件夹中的文件与流(即不知道文件的数量)。正因为如此,我试图用ZipInputStream重写方法(在发布之前,我在寻找解决方案时发现这是推荐的)。这就是现在的方法:

  imageMap = new HashMap< String,BufferedImage>(); 
String imagebase =/ images;
CodeSource src = PlatformerCanvas.class.getProtectionDomain()。getCodeSource();
if(src!= null)
{
URL jar = src.getLocation();
尝试
{
ZipArchiveInputStream zip = new ZipArchiveInputStream(jar.openStream());
ZipArchiveEntry ze = null; ((ze = zip.getNextZipEntry())!= null)


System.out.println(ze.getName());
if(ze.getName()。startsWith(imagebase)&& ze.getName()。endsWith(。png))
{
loading ++;
imageMap.put(ze.getName()。replaceAll(imagebase +/,),ImageIO.read(this.getClass()。getResource(ze.getName())));
}
}
zip.close();
System.out.println(Finished Loading Images);

catch(IOException e)
{
e.printStackTrace();






$ b

然而,这完全跳过了while循环,因为getNextZipEntry返回null。我尝试使用基本的Zip归档库,但有人推荐Apache Commons库,但都没有工作。我的罐子设置是这样的:

  Jar 
/ game / gameClasses
/ images / imageFiles
/ sounds / soundFiles
/ whateverOtherFoldersEclipseCreated

所以我的问题是这样的:为什么不这个目前的方法工作,我该如何解决?或者,如何以不同的方式加载图像/声音?
我看了很多其他的问题,尝试了很多不同的东西,但是都没有为我工作。 $ b

或者,我怎样才能以不同的方式加载图像/声音?

一个常见的技术是包括Jar中已知位置的资源列表。阅读列表,然后迭代这些行(假设每个资源一行)&阅读这些路径。


I'm writing a game in java and all is working more or less well, as you would expect when coding, but one issue I have come across is reading the image and sound files into the game. I wrote a method that works really well within Eclipse, but as soon as I try to test it in a runnable jar (exported using Eclipse) my game gets stuck on the loading screen because it can't read the files. I realized the problem there being that you cannot create File objects within a Jar, you have to use streams. This could work, but the images/sounds are located within folders and I'm not very sure how you can read files within a folder with streams(i.e not knowing the amount of files). Because of this I have tried to rewrite the method using the ZipInputStream (I found this as recommended when looking for solutions prior to posting this). This is what the method looks like now:

imageMap = new HashMap<String, BufferedImage>();
    String imagebase = "/images";
    CodeSource src = PlatformerCanvas.class.getProtectionDomain().getCodeSource();
    if(src != null)
    {
        URL jar = src.getLocation();
        try
        {
            ZipArchiveInputStream zip = new ZipArchiveInputStream(jar.openStream());
            ZipArchiveEntry ze = null;
            while((ze = zip.getNextZipEntry()) != null)
            {
                System.out.println(ze.getName());
                if(ze.getName().startsWith(imagebase) && ze.getName().endsWith(".png"))
                {
                    loading++;
                    imageMap.put(ze.getName().replaceAll(imagebase + "/", ""), ImageIO.read(this.getClass().getResource(ze.getName())));
                }
            }
            zip.close();
            System.out.println("Finished Loading Images");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

However, this completely skips over the while loop because getNextZipEntry return null. I tried using the base Zip archive libraries, but someone recommended the Apache Commons libraries, but neither worked. My Jar setup is this:

Jar
/game/gameClasses
/images/imageFiles
/sounds/soundFiles
/whateverOtherFoldersEclipseCreated

So my question is this: Why isn't this current method working and how do I fix it? Or, how can I load the images/sounds in a different way that works? I have looked at many other questions and tried many different things, but none worked for me.

解决方案

Or, how can I load the images/sounds in a different way that works?

One common technique is to include a list of resources in a known location in the Jar. Read the list, then iterate the lines (presuming one line per resource) & read the paths that way.

这篇关于在Jar中读取文件(ZipInputStream等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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