Search code examples
linuxwindowsjarfilenameslong-filenames

How to open files in JAR file with filename length greater than 255?


I have a JAR file with following structure:

com
-- pack1
   -- A.class
-- pack2
   -- AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.class

When I try to read, extract or rename pack2/AA...AA.class (which has a 262 byte long filename) both Linux and Windows say filename is too long. Renaming inside the JAR file doesn't also work.

Any ideas how to solve this issue and make the long class file readable?


Solution

  • java.util.jar can handle it:

    try {
        JarFile jarFile = new JarFile("/path/to/target.jar");
        Enumeration<JarEntry> jarEntries = jarFile.entries();
        int i = 0;
        while (jarEntries.hasMoreElements()) {
            JarEntry jarEntry = jarEntries.nextElement();
            System.out.println("processing entry: " + jarEntry.getName());
            InputStream jarFileInputStream = jarFile.getInputStream(jarEntry);
            OutputStream jarOutputStream = new FileOutputStream(new File("/tmp/test/test" + (i++) + ".class")); // give temporary name to class
            while (jarFileInputStream.available() > 0) {
                jarOutputStream.write(jarFileInputStream.read());
            }
            jarOutputStream.close();
            jarFileInputStream.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(JARExtractor.class.getName()).log(Level.SEVERE, null, ex);
    }
    

    The output willbe test<n>.class for each class.