gpt4 book ai didi

java - 如何在 java 中获取 .exe 文件的最后 'n' 字节?

转载 作者:行者123 更新时间:2023-11-30 06:56:57 25 4
gpt4 key购买 nike

我需要在 java 中自解压 .exe 文件中获取最后 22 个字节作为中央目录的末尾(没有命令行,请没有终端解决方案)。我尝试使用 bufferInputStream 读取 .exe 文件的内容并获得成功,但是当尝试使用

获取最后 22 个字节时
BufferInputStream.read(byteArray, 8170, 22);

java 引发异常说它是封闭流。在这方面的任何帮助将不胜感激。谢谢。

最佳答案

我没有试过这个,但我想你可以使用 MappedByteBuffer 来只读取最后 22 个字节。

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

然后只需将缓冲区刷新到数组中即可。

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

关于java - 如何在 java 中获取 .exe 文件的最后 'n' 字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33916100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com