Fast way to update text in a Jar file

本秂侑毒 提交于 2019-12-23 05:39:11

问题


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();

}

来源:https://stackoverflow.com/questions/25735374/fast-way-to-update-text-in-a-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!