How to list the files inside a JAR file?

前端 未结 16 2244
温柔的废话
温柔的废话 2020-11-22 00:40

I have this code which reads all the files from a directory.

    File textFolder = new File(\"text_directory\");

    File [] texFiles = textFolder.listFiles         


        
相关标签:
16条回答
  • 2020-11-22 01:32

    Here's a method I wrote for a "run all JUnits under a package". You should be able to adapt it to your needs.

    private static void findClassesInJar(List<String> classFiles, String path) throws IOException {
        final String[] parts = path.split("\\Q.jar\\\\E");
        if (parts.length == 2) {
            String jarFilename = parts[0] + ".jar";
            String relativePath = parts[1].replace(File.separatorChar, '/');
            JarFile jarFile = new JarFile(jarFilename);
            final Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                final JarEntry entry = entries.nextElement();
                final String entryName = entry.getName();
                if (entryName.startsWith(relativePath)) {
                    classFiles.add(entryName.replace('/', File.separatorChar));
                }
            }
        }
    }
    

    Edit: Ah, in that case, you might want this snippet as well (same use case :) )

    private static File findClassesDir(Class<?> clazz) {
        try {
            String path = clazz.getProtectionDomain().getCodeSource().getLocation().getFile();
            final String codeSourcePath = URLDecoder.decode(path, "UTF-8");
            final String thisClassPath = new File(codeSourcePath, clazz.getPackage().getName().repalce('.', File.separatorChar));
        } catch (UnsupportedEncodingException e) {
            throw new AssertionError("impossible", e);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 01:33
    CodeSource src = MyClass.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();
        if (name.startsWith("path/to/your/dir/")) {
          /* Do something with this entry. */
          ...
        }
      }
    } 
    else {
      /* Fail... */
    }
    

    Note that in Java 7, you can create a FileSystem from the JAR (zip) file, and then use NIO's directory walking and filtering mechanisms to search through it. This would make it easier to write code that handles JARs and "exploded" directories.

    0 讨论(0)
  • 2020-11-22 01:36

    Given an actual JAR file, you can list the contents using JarFile.entries(). You will need to know the location of the JAR file though - you can't just ask the classloader to list everything it could get at.

    You should be able to work out the location of the JAR file based on the URL returned from ThisClassName.class.getResource("ThisClassName.class"), but it may be a tiny bit fiddly.

    0 讨论(0)
  • 2020-11-22 01:37

    Code that works for both IDE's and .jar files:

    import java.io.*;
    import java.net.*;
    import java.nio.file.*;
    import java.util.*;
    import java.util.stream.*;
    
    public class ResourceWalker {
        public static void main(String[] args) throws URISyntaxException, IOException {
            URI uri = ResourceWalker.class.getResource("/resources").toURI();
            Path myPath;
            if (uri.getScheme().equals("jar")) {
                FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());
                myPath = fileSystem.getPath("/resources");
            } else {
                myPath = Paths.get(uri);
            }
            Stream<Path> walk = Files.walk(myPath, 1);
            for (Iterator<Path> it = walk.iterator(); it.hasNext();){
                System.out.println(it.next());
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 01:38

    I've ported acheron55's answer to Java 7 and closed the FileSystem object. This code works in IDE's, in jar files and in a jar inside a war on Tomcat 7; but note that it does not work in a jar inside a war on JBoss 7 (it gives FileSystemNotFoundException: Provider "vfs" not installed, see also this post). Furthermore, like the original code, it is not thread safe, as suggested by errr. For these reasons I have abandoned this solution; however, if you can accept these issues, here is my ready-made code:

    import java.io.IOException;
    import java.net.*;
    import java.nio.file.*;
    import java.nio.file.attribute.BasicFileAttributes;
    import java.util.Collections;
    
    public class ResourceWalker {
    
        public static void main(String[] args) throws URISyntaxException, IOException {
            URI uri = ResourceWalker.class.getResource("/resources").toURI();
            System.out.println("Starting from: " + uri);
            try (FileSystem fileSystem = (uri.getScheme().equals("jar") ? FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap()) : null)) {
                Path myPath = Paths.get(uri);
                Files.walkFileTree(myPath, new SimpleFileVisitor<Path>() { 
                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        System.out.println(file);
                        return FileVisitResult.CONTINUE;
                    }
                });
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 01:38

    A jar file is just a zip file with a structured manifest. You can open the jar file with the usual java zip tools and scan the file contents that way, inflate streams, etc. Then use that in a getResourceAsStream call, and it should be all hunky dory.

    EDIT / after clarification

    It took me a minute to remember all the bits and pieces and I'm sure there are cleaner ways to do it, but I wanted to see that I wasn't crazy. In my project image.jpg is a file in some part of the main jar file. I get the class loader of the main class (SomeClass is the entry point) and use it to discover the image.jpg resource. Then some stream magic to get it into this ImageInputStream thing and everything is fine.

    InputStream inputStream = SomeClass.class.getClassLoader().getResourceAsStream("image.jpg");
    JPEGImageReaderSpi imageReaderSpi = new JPEGImageReaderSpi();
    ImageReader ir = imageReaderSpi.createReaderInstance();
    ImageInputStream iis = new MemoryCacheImageInputStream(inputStream);
    ir.setInput(iis);
    ....
    ir.read(0); //will hand us a buffered image
    
    0 讨论(0)
提交回复
热议问题