Getting a directory inside a .jar

后端 未结 8 1484
悲哀的现实
悲哀的现实 2020-12-11 04:48

I am trying to access a directory inside my jar file. I want to go through every of the files inside the directory itself. I tried, for example, using the following:

<
相关标签:
8条回答
  • 2020-12-11 04:48

    I think you can directly access resources in ZIP/JAR file Please see Tutorial its giving solution to your question

    How to extract Java resources from JAR and zip archives

    Hopes that helps

    0 讨论(0)
  • 2020-12-11 04:54

    What you are looking for here might be the JarEntry list of the Jar... I had done some similar work during grad school... You can get the modified class here (http://code.google.com/p/marcellodesales-cs-research/source/browse/trunk/grad-ste-ufpe-brazil/ptf-add-on-dev/src/br/ufpe/cin/stp/global/filemanager/JarFileContentsLoader.java) Note that the URL contains an older Java class not using Generics...

    This class returns a set of URLs with the protocol "jar:file:/" for a given token...

    package com.collabnet.svnedge.discovery.client.browser.util;
    
    import java.io.IOException;
    import java.net.URL;
    import java.util.Enumeration;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.jar.JarEntry;
    import java.util.jar.JarFile;
    
    public class JarFileContentsLoader {
    
        private JarFile jarFile;
    
        public JarFileContentsLoader(String jarFilePath) throws IOException {
            this.jarFile = new JarFile(jarFilePath);
        }
    
        /**
         * @param existingPath an existing path string inside the jar.
         * @return the set of URL's from inside the Jar (whose protocol is "jar:file:/"
         */
        public Set<URL> getDirEntries(String existingPath) {
            Set<URL> set = new HashSet<URL>();
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                String element = entries.nextElement().getName();
                URL url = getClass().getClassLoader().getResource(element);
                if (url.toString().contains("jar:file")
                        && !element.contains(".class")
                        && element.contains(existingPath)) {
                    set.add(url);
                }
            }
            return set;
        }
    
        public static void main(String[] args) throws IOException {
            JarFileContentsLoader jarFileContents = new JarFileContentsLoader(
                    "/u1/svnedge-discovery/client-browser/lib/jmdns.jar");
            Set<URL> entries = jarFileContents.getDirEntries("impl");
            Iterator<URL> a = entries.iterator();
            while (a.hasNext()) {
                URL element = a.next();
                System.out.println(element);
            }
        }
    
    }
    

    The output would be:

    jar:file:/u1/svnedge-discovery/client-browser/lib/jmdns.jar!/javax/jmdns/impl/constants/
    jar:file:/u1/svnedge-discovery/client-browser/lib/jmdns.jar!/javax/jmdns/impl/tasks/state/
    jar:file:/u1/svnedge-discovery/client-browser/lib/jmdns.jar!/javax/jmdns/impl/tasks/resolver/
    jar:file:/u1/svnedge-discovery/client-browser/lib/jmdns.jar!/javax/jmdns/impl/
    jar:file:/u1/svnedge-discovery/client-browser/lib/jmdns.jar!/javax/jmdns/impl/tasks/
    
    0 讨论(0)
  • 2020-12-11 04:54

    IF you really want to treat JAR files like directories, then please have a look at TrueZIP 7. Something like the following might be what you want:

    URL url = ... // whatever
    URI uri = url.toURI();
    TFile file = new TFile(uri); // File-look-alike in TrueZIP 7
    if (file.isDirectory) // true for regular directories AND JARs if the module truezip-driver-file is on the class path
        for (TFile entry : file.listFiles()) // iterate top level directory
             System.out.println(entry.getPath()); // or whatever
    

    Regards, Christian

    0 讨论(0)
  • 2020-12-11 05:01

    May the following code sample can help you

       Enumeration<URL> inputStream = BrowserFactory.class.getClassLoader().getResources(".");
            System.out.println("INPUT STREAM ==> "+inputStream);
            System.out.println(inputStream.hasMoreElements());
            while (inputStream.hasMoreElements()) {
                URL url = (URL) inputStream.nextElement();
                System.out.println(url.getFile());
            }
    
    0 讨论(0)
  • 2020-12-11 05:02

    If I understand your problem you want to check the directory inside the jar and check all the files inside that directory.You can do something like:

    JarInputStream jar = new JarInputStream(new FileInputStream("D:\\x.jar"));
        JarEntry jarEntry ;
        while(true)
            {
             jarEntry = jar.getNextJarEntry();
             if(jarEntry != null)
             {
    
                if(jarEntry.isDirectory() == false)
                {
            String str = jarEntry.getName();
                    if(str.startsWith("weblogic/xml/saaj"))
            {
                anything which comes here are inside weblogic\xml\saaj directory
            }
            }
    
         }
        }    
    
    0 讨论(0)
  • 2020-12-11 05:05

    Paths within Jars are paths, not actual directories as you can use them on a file system. To get all resources within a particular path of a Jar file:

    • Gain an URL pointing to the Jar.
    • Get an InputStream from the URL.
    • Construct a ZipInputStream from the InputStream.
    • Iterate each ZipEntry, looking for matches to the desired path.

    ..will I still be able to test my Applet when it's not inside that jar? Or will I have to program two ways to get my Images?

    The ZipInputStream will not work with loose resources in directories on the file system. But then, I would strongly recommend using a build tool such as Ant to build (compile/jar/sign etc.) the applet. It might take an hour or so to write the build script & check it, but thereafter you can build the project by a few keystrokes and a couple of seconds.

    It would be quite annoying if I always have to extract and sign my jar if I want to test my Aplet

    I'm not sure what you mean there. Where does the 'extract' come into it? In case I was not clear, a sand-boxed applet can load resources this way, from any Jar that is mentioned in the archive attribute. Another thing you might do, is to separate the resource Jar(s) from the applet Jar. Resources typically change less than code, so your build might be able to take some shortcuts.

    I think I really have to consider putting my Images into a seperate directory outside the jar.

    If you mean on the server, there will be no practical way to get a listing of the image files short of help from the server. E.G. Some servers are insecurely set up to produce an HTML based 'file list' for any directory with no default file (such as an index.html).


    I have only got one jar, in which my classes, images and sounds are.

    OK - consider moving the sounds & images into a separate Jar. Or at the very least, put them in the Jar with 'no compression'. While Zip comression techniques work well with classes, they are less efficient at compressing (otherwise already compressed) media formats.

    I have to sign it because I use the "Preferences" class to save user settings."

    There are alternatives to the Preferences for applets, such as cookies. In the case of plug-in 2 architecture applet, you can launch the applet (still embedded in the browser) using Java Web Start. JWS offers the PersistenceService. Here is my small demo. of the PersistenceService.

    Speaking of JWS, that brings me to: Are you absolutely certain this game would be better as an applet, rather than an app (e.g. using a JFrame) launched using JWS?

    Applets will give you no end of stress, and JWS has offered the PersistenceService since it was introduced in Java 1.2.

    0 讨论(0)
提交回复
热议问题