ZipFile zip = new ZipFile("Outer.zip");
...
zip.getInputStream(ze); // It is giving null
Содержимое ze
(например, TxtFile1.txt
) является частью InnerZip.zip
, а не Outer.zip
(представлено zip
), следовательно, null
.
Я бы использовалрекурсия:
public static void main(String[] args) throws IOException {
String name = "Outer.zip";
FileInputStream input = new FileInputStream(new File(name));
readZip(input, name);
}
public static void readZip(final InputStream in, final String name) throws IOException {
final ZipInputStream zin = new ZipInputStream(in);
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
if (entry.getName().toLowerCase().endsWith(".zip")) {
readZip(zin, name + "/" + entry.getName());
} else {
readFile(zin, entry.getName());
}
}
}
private static void readFile(final InputStream in, final String name) {
String contents = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));
System.out.println(String.format("Contents of %s: %s", name, contents));
}
0. while (...)
мы перебираем все записи целиком.
1. (if (.endsWith(".zip"))
) весли мы встречаемся с другим почтовым индексом, мы вызываем его рекурсивно (readZip()
) и переходим к шагу 0.
2. (else
), в противном случае мы печатаем содержимоефайла (при условии, текстовые файлы здесь).