java - 无法从 ZIP 文件输入流中读取文件
问题描述
我有一个要阅读的 Zip 文件。我不想使用 a ZipFile
,因为将来我想对不是来自文件的数据执行此操作。
这是我到目前为止所尝试的。它不打印 的内容res00000.dat
,而是打印一个空行。我不知道如何解决这个问题
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
if (!zipEntry.getName().equals("res00000.dat")) {
zipInputStream.closeEntry();
continue;
}
}
int len;
ByteArrayOutputStream byteArrayOutputStream = new ByterrayOutputStream();
byte[] buffer = new byte[1024];
while ((len = zipInputStream.read(buffer)) > 0) {
byteArrayOutputStream.write(buffer, 0, len);
}
String xml = byteArrayOutputStream.toString();
System.out.println(xml);
zipInputStream.closeEntry();
zipInputStream.close();
return null;
我的 ZIP 文件中只有两个文件。这是我试图解析的 Blackboard Test bank 文件:
Zip file
+-imsmanifest.xml
+-res00000.dat
有人可以帮忙吗?
解决方案
您的代码当前不处理丢失的条目。它只是默默地滚动到 a 的末尾,ZipInputStream
所以没有办法知道发生了什么。当名称标识的条目丢失时,您可以执行以下操作以获取异常:
public String readEntry(ZipInputStream in, String name) {
while ((zipEntry = in.getNextEntry()) != null) {
if (zipEntry.getName().equals(name)) {
return readXml(zipInputStream);
}
}
throw new IllegalStateException(name + " not found inside ZIP");
}
您IllegalStateException
现在很可能会在上面观察到缺少res00000.dat
.
请注意,在滚动时没有理由closeEntry()
手动调用,ZipInputStream
因为getNextEntry()
它已经在引擎盖下进行了。来自 JDK 11 源代码:
public ZipEntry getNextEntry() throws IOException {
ensureOpen();
if (entry != null) {
closeEntry();
}
...
推荐阅读
- loops - 如何在 JMeter 中为每个用户动态迭代请求
- nestjs - 在 Nestjs 中使用 multipart/form-data
- oauth - 生成的 Jwt 令牌没有 . 点
- android - 我一直在尝试显示我的数据库的内容,但是我不断收到此错误“java.lang.String cannot be cast to java.util.HashMap”
- flutter - 仅当您在颤动的导航堆栈中的最后一个基本窗口中时,如何防止android后退按钮关闭应用程序?
- c++ - #ifndef 错误:需要更新包含路径
- variables - 在 csh 中,如何使用变量修饰符从“a/b/c/”生成“a/b”?
- javascript - javascript函数在它应该是随机的时返回相同的值
- javascript - 如何在另一个表单元素中显示 jquery 的结果?
- c++ - 使用自定义迭代器基于另一个容器对容器进行排序