Fast way to update text in a Jar file

2019-06-10 00:14发布

问题:

I have a c++ executable with a Jar file embedded as a resource. The Jar contains a plaintext file within. I need to update a single line of unique text in the plaintext file with another line of the same length. I think the best solution would be to simply open the Exe with a RandomAccessFile and overwrite the text, however, when I open the Exe or the Jar in a hex editor, I cannot find the unique text. My guess is that the text is encoded somehow.

In short: How can I change text from a plaintext file in a jar file embedded in a .exe?

Here is the code im using to overwrite the text in the exe/jar. The code doesnt actually overwrite the line because it reaches the end of file before a match is found.

public static void updateOptions(String line, File target) {
    String code = "long unique text";

    RandomAccessFile raf = new RandomAccessFile(target, "rwd");

    // find the unique text and replace with the new text
    for (long l = 0; l < raf.length(); l++) {
        raf.seek(l);
        boolean equal = true;
        for (int c = 0; c < code.length(); c++) {
            if (raf.readChar() != code.charAt(c)) {
                equal = false;
                break;
            }
        }
        if (equal) {
            // found the match
            raf.seek(l);
            raf.write(line.getBytes());
            raf.write("\n".getBytes());
            break;

        } else {
            // keep looking
            continue;
        }

    }
    raf.close();

}