Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to _directly_ get a list of files from a sub-directory in a .JAR - JAVA
    primarykey
    data
    text
    <p>I'm looking for a way to <em>directly</em> get a list of all files within a specified sub-directory within a .jar.</p> <p>This is similar: <a href="https://stackoverflow.com/questions/1429172/list-files-inside-a-jar-file">How do I list the files inside a JAR file?</a></p> <p>But unfortunately the method listed above will iterate over all files in the entire .jar. When working with a .jar with many files in it - this seems impractical as is my case. There has to be a way to directly path to a sub directory within the .jar and iterate over its contents, without having to iterate over all contents of the jar and then filter the results to find those entries that you care about.</p> <p>Here is roughly what I have:</p> <pre><code>public static ArrayList&lt;String&gt; loadInternalFileListing(MetaDataType type, MetaDataLocation location, String siteId) { ArrayList&lt;String&gt; filenameList = new ArrayList&lt;String&gt;(); URL url = getInternalFile(type, null, location, siteId); JarURLConnection juc = null; JarFile jarFile = null; try { juc = (JarURLConnection) url.openConnection(); jarFile = juc.getJarFile(); Enumeration&lt;JarEntry&gt; entries = jarFile.entries(); for(JarEntry jarEntry = entries.nextElement(); entries.hasMoreElements(); jarEntry = entries.nextElement()) { ...//logic here } } ... //catch, handle exceptions, finally block and other logic return filenameList; } </code></pre> <p>The variable: <strong><em>url</em></strong> ->: jar:file:/C:/jboss-4.2.2.GA/server/username/tmp/deploy/tmp8421264930467110704SomeEar.ear-contents/SomeBusiness.jar!/subdir1/subdir2/subdir3/subdir4/</p> <p>The path: /subdir1/subdir2/subdir3/subdir4/ is exactly where I want to iterate.</p> <p>Debugging reveals that <strong><em>juc</em></strong> is indeed created correctly and pointing to the correct path. <strong><em>jarFile</em></strong> then as expected, just gives me the jar file path which is why I lose the subdirectories and why I start iterating at the root. This all makes sense. This clearly isn't the correct way. There has to be another way!</p> <p>Messing around with the <em>JarURLConnection</em> which theoretically points to the correct directory I am interested in, doesn't reveal anything useful. There is <em>JarURLConnection.getInputStream()</em>. Debugger indicates that this ultimately holds a <em>ZipFileInputStream</em>, but I can't get access to it, and looking further it looks like its just the <em>ZipFileInputStream</em> to the <em>ZipFile</em> - which puts me back at square one.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload