您不能使用TarArchiveEntry的getFile.当你压缩tar文件中的文件时,那个getter仅用于相反的操作.
相反,您应该直接从TarArchiveInputStream中读取.它将负责返回“动态”解压缩它的“文件”的内容.
例如(未经测试的代码,YMMV):
TarArchiveInputStream tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream("c://temp//test.tar.gz")));
TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
while (currentEntry != null) {
br = new BufferedReader(new InputStreamReader(tarInput)); // Read directly from tarInput
System.out.println("For File = " + currentEntry.getName());
String line;
while ((line = br.readLine()) != null) {
System.out.println("line="+line);
}
currentEntry = tarInput.getNextTarEntry(); // You forgot to iterate to the next file
}