【发布时间】:2021-10-22 03:23:04
【问题描述】:
我想知道是否有任何有效的方法可以从一个非常大的 zip 文件中读取一个小文件。我对 zip 中的任何其他文件不感兴趣,除了一个名为 inventory.xml 的小文件。
确切地说,zip 文件位于 artifactory 中。所以我也不想将整个文件下载到我的磁盘中。这是我现在所拥有的。
URL url = new URL("artifactory-url");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
int status = con.getResponseCode();
if (status != 200) {
System.out.println("Unable to find the artifact : " + url.toString());
return bugs;
}
try (ZipInputStream zipStream = new ZipInputStream(con.getInputStream())) {
ZipEntry entry;
while ((entry = zipStream.getNextEntry()) != null) {
if (entry.getName().contains("inventory.xml")) {
//do something here
}
}
}
另一个问题是,如果我知道文件的坐标,会有帮助吗?
【问题讨论】:
-
这段代码效率不高吗?
-
@DhanasekaranDon 它遍历每个 zipentry,如果它是一个包含很多条目的巨大 zip 文件,则需要很长时间。
-
对此您无能为力。这是一种顺序格式。确保在处理完所需文件后跳出循环。
-
con.getInputStream()获取完整文件,因此您不能只获取文件的一部分。正如@user207421 所说,您对此无能为力。 -
@Renis1235
getInputStream()返回输入流。然后,您必须根据需要从中读取数据。您还没有下载整个文件。只是您阅读的部分。
标签: java zip zipinputstream