读取 zip 存档中的文本文件
我有zip存档,其中包含一堆纯文本文件。我想解析每个文本文件数据。以下是我到目前为止所写的内容:
try {
final ZipFile zipFile = new ZipFile(chooser.getSelectedFile());
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
ZipInputStream zipInput = null;
while (entries.hasMoreElements()) {
final ZipEntry zipEntry = entries.nextElement();
if (!zipEntry.isDirectory()) {
final String fileName = zipEntry.getName();
if (fileName.endsWith(".txt")) {
zipInput = new ZipInputStream(new FileInputStream(fileName));
final RandomAccessFile rf = new RandomAccessFile(fileName, "r");
String line;
while((line = rf.readLine()) != null) {
System.out.println(line);
}
rf.close();
zipInput.closeEntry();
}
}
}
zipFile.close();
}
catch (final IOException ioe) {
System.err.println("Unhandled exception:");
ioe.printStackTrace();
return;
}
我是否需要 RandomAccessFile 来执行此操作?我在拥有ZipInputStream的地方迷路了。