在Jar中读取文件(ZipInputStream等) [英] Reading Files Within a Jar(ZipInputStream, etc.)
问题描述
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屋!