gpt4 book ai didi

Java 压缩文件/字节数组问题

转载 作者:行者123 更新时间:2023-11-29 05:30:02 26 4
gpt4 key购买 nike

我有一个 zip 文件,其中包含 3 个相对较大(几 mb)的二进制文件,我需要读入缓冲区以执行一些正则表达式操作。

//For entry in zip.getentries loop above

long size = entry.getSize();

byte[] bytes = new byte[(int) size];

int numBytesRead = zipFile.getInputStream(entry).read(bytes);

if (numBytesRead != size) {
Logger.getLogger(this.getClass()).debug(
"Read " + numBytesRead + " bytes from zip entry but size of the
zip entry is " + size + " bytes");
//continue; - If I do not comment this out the size check fails and nothing happens
}

FileUtils.writeByteArrayToFile(extractFile, bytes);

出于某种原因,我只将大约 1/15 左右的数据放入字节数组(在一个示例中,642 条记录中有 43 条记录)。我最初认为这可能是将大小转换为 int 的问题,但大小通常在 1,000,000 - 10,000,000 之间,所以这不是问题。有什么想法吗?

最佳答案

InputStream.read 不需要读取所有 数据。它将读取一些数据,数量在 1 和数组大小之间变化。

您将不得不重复调用该函数,直到您读取了所有需要的数据。

作为引用,请检查 API docs : 他们明确表示“可能会读取较小的数字”。

关于Java 压缩文件/字节数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21433959/

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