How to read a jar file, convert it to a string and create a new jar file from that string?

StackOverflow https://stackoverflow.com/questions/23313854

  •  10-07-2023
  •  | 
  •  

Question

I´m trying to implement some "over the air" update mechanism for OSGi bundles. For that, I need to be able to create a jar file from a String (basically the content of the jar file read by JarInputStream). The following example code should illustrate my needs:

                //read bundle to be copied!
                File originalFile = new File(
                        "/Users/stefan/Documents/Projects/OSGi/SimpleBundle_1.0.0.201404.jar");
                JarInputStream fis = new JarInputStream(new FileInputStream(originalFile));
                StringBuilder stringBuilder = new StringBuilder();
                int ch;
                while ((ch = fis.read()) != -1) {
                    stringBuilder.append((char) ch);
                }

                fis.close();

                //Create content string
                String content = stringBuilder.toString();
                if (logger.isInfoEnabled()) {
                    logger.info(content);
                }

                //Init new jar input stream
                JarInputStream jarInputStream = new JarInputStream(
                        new ByteArrayInputStream(content.getBytes()));

                if (logger.isInfoEnabled()) {
                    logger.info("Save content to disc!");
                }

                File newFile = new File(
                        "/Users/stefan/Documents/Projects/OSGi/equinox/SimpleBundle_1.0.0.201404.jar");
                //Init new jar output stream
                JarOutputStream fos = new JarOutputStream(
                        new FileOutputStream(newFile));

                if (!newFile.exists()) {
                    newFile.createNewFile();
                }

                int BUFFER_SIZE = 10240;
                byte buffer[] = new byte[BUFFER_SIZE];

                while (true) {

                    int nRead = jarInputStream.read(buffer, 0,
                            buffer.length);
                    if (nRead <= 0)
                        break;

                    fos.write(buffer, 0, nRead);
                }

                //Write content to new jar file.
                fos.flush();
                fos.close();
                jarInputStream.close();

Unfortunately, the created jar file is empty and throws an "Invalid input file" error if I try to open it with JD-GUI. Is it possible to create a jar file from the String "content"?

Best regards and thank you very much Stefan

Was it helpful?

Solution

Your jar is empty because you do not read anything from the JarInputStream. If you want to read JarInputStream, you should iterate its entries. If you want to change the Manifest, the first entry should be skipped, use the getManifest() of the jarInputStream and the constructor of the JarOutputStream, where Manifest can be specified. Based on your code (no manifest change but plain jar copy):

ZipEntry zipEntry = jarInputStream.getNextEntry();
while (zipEntry != null) {
    fos.putNextEntry(zipEntry);

    // Simple stream copy comes here
    int BUFFER_SIZE = 10240;
    byte buffer[] = new byte[BUFFER_SIZE];
    int l = jarInputStream.read(buffer);
    while(l >= 0) {
        fos.write(buffer, 0, l);
        l = jarInputStream.read(buffer);
    }

    zipEntry = jarInputStream.getNextEntry();
}

You only need this if you want to change the content (Manifest or entries) of the JAR file during the copy. Otherwise, simple InputStream and FileOutputStream will do the work (as Tim said).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top