我正在尝试从 a 读取单个文件java.util.zip.ZipInputStream
,并将其复制到 ajava.io.ByteArrayOutputStream
中(这样我就可以创建 ajava.io.ByteArrayInputStream
并将其交给最终关闭流的 3rd 方库,我不希望我ZipInputStream
被关闭) .
我可能在这里遗漏了一些基本的东西,但我从来没有在这里进入 while 循环:
ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream();
int bytesRead;
byte[] tempBuffer = new byte[8192*2];
try {
while ((bytesRead = zipStream.read(tempBuffer)) != -1) {
streamBuilder.write(tempBuffer, 0, bytesRead);
}
} catch (IOException e) {
// ...
}
我错过了什么可以让我复制流?
编辑:
我之前应该提到这ZipInputStream
不是来自文件,所以我认为我不能使用ZipFile
. 它来自通过 servlet 上传的文件。
另外,我已经在访问这段代码之前调用getNextEntry()
过。ZipInputStream
如果我不尝试将文件复制到另一个InputStream
(通过OutputStream
上面提到的),而只是传递ZipInputStream
给我的第 3 方库,库将关闭流,我不能再做任何事情,比如处理剩余的文件流。