javaexeself-extracting

How to get last 'n' bytes of .exe file in java?


I need to get last 22 Bytes as end of central directory in self extracting .exe file in java (No command line, No terminal solutions please). I tried to read content of .exe file using bufferInputStream and got successful but when trying to get last 22 bytes using

BufferInputStream.read(byteArray, 8170, 22);

java is raising exception saying it is closed stream.Any help in this regard would be much appreciated. Thanks.


Solution

  • I haven't tried this, but I suppose you could use a MappedByteBuffer to read just the last 22 bytes.

    File file = new File("/path/to/my/file.bin");
    
    long size = file.length();
    FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
    MappedByteBuffer buffer = channel.map(MapMode.READ_ONLY, size-22, 22);
    

    Then simply flush your buffer into an array and that's it.

    byte[] payload = new byte[22];
    buffer.get(payload);