喜欢这个网站!我的问题如下:
我正在读取来自 HTTP“PUT”请求的网络的 zip 文件。请求标头告诉我 Content-Length 是(比如说)1Mb。以下代码创建 ZipInputStream,并将 zip 内容保存到当前目录中的文件中:
ZipInputStream zis = new ZipInputStream(inputStream);
ZipEntry ze;
long totalBytesRead = 0;
while ((ze = zis.getNextEntry()) != null) {
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(ze.getName()));
byte[] buffer = new byte[4096];
int i;
while ((i = zis.read(buffer)) != -1) {
totalBytesRead+=i;
outStream.write(buffer,0,i);
}
outStream.close();
}
inputStream.close();
总而言之,totalBytesRead
大约等于 1.5Mb(取决于文件的压缩,可能是任何东西!)。我想知道的是,是否有办法找出从原始文件中读取了多少实际字节inputStream
?两者都ze.getSize()
为ze.getCompressedSize()
每个压缩条目返回 -1(即它不知道)。我需要此信息作为进度条,以显示已从网络读取传输的 zip 文件的字节数。
建议?我是否应该将 ZipInputStream 子类化并尝试找出它从包装的 InputStream 中读取的字节数?
提前致谢!