I have a jar file that contains a folder structure ie...
jar/com/foo/bar/images
Inside the Images directory I have several images. I have tried to read them out with getResourceAsStream() and also attempted to get at them via ZipEntries.
All of my attempts have been fruitless, if someone could please give me an example on how this would be done.
P.S don't worry about closing resources and error handling etc. I will be able to put that part in.
Thanks for the help.
here is the zipfile entry code I tried
private File[] getFilesFromPath(String location) {
File[] files = null;
try {
CodeSource src = this.getClass().getProtectionDomain().getCodeSource();
if (src != null) {
URL jar = src.getLocation();
ZipInputStream zip = new ZipInputStream(jar.openStream());
while(true) {
ZipEntry e = zip.getNextEntry();
if (e == null)
break;
String name = e.getName();
if (name.endsWith(location)) {
files = new File(name).listFiles((new FileFilter() {
@Override
public boolean accept(File f) {
return f.isFile();
}
}));
for (File file : files){
System.out.println(file.getName());
}
}
}
}
}catch (IOException e) {
e.printStackTrace();
}
return files;
}
The following code should work in your case as I already used following code to read hbm files from an existing jar to create dynamic session factory. It is simply reading the jar file and yhen looking image folder for given location(com/foo/bar/images) then it assume that all the file in this folder are image file. It convert file to Input stream.
Change the JARCLASSNAME to the one of your class name from jar file.
CodeSource src = JARCLASSNAME.class.getProtectionDomain().getCodeSource();
if (src != null) {
URL jar = src.getLocation();
ZipInputStream zip = new ZipInputStream(jar.openStream());
while(true) {
ZipEntry e = zip.getNextEntry();
if (e == null)
break;
String name = e.getName();
// Check the folder name if folder name match then only read the file
if (name.startsWith("com/foo/bar/images")) {
InputStream inputStream =JARCLASSNAME.class.getClassLoader().getResourceAsStream(name);
}
}
}