```java
/**
* 解析tar存档
*
* @param inputStream 输入流
* @return {@link StringBuilder}
* @throws IOException ioexception
*/
private StringBuilder parseTarArchive(InputStream inputStream) throws IOException {
//创建临时目录
File tempDir = Files.createTempDirectory("tarTempDir").toFile();
unpackTarFile(inputStream, tempDir);
// Now we can process ETK log files recursively.
StringBuilder result = new StringBuilder();
//获取tar压缩包中路径文件夹中的文件用来解析
File file = new File(tempDir, "tmp/FL0/ETK");
processFilesInDir(result, file);
return result;
}
/**
* 解压tar文件
*
* @param inputStream 输入流
* @param destDir 文件
* @throws IOException ioexception
*/
private void unpackTarFile(InputStream inputStream, File destDir) throws IOException {
try (TarArchiveInputStream tarStream = new TarArchiveInputStream(inputStream)) {
TarArchiveEntry entry;
byte[] buffer = new byte[1024]; // 临时字节数组
while ((entry = tarStream.getNextTarEntry()) != null) {
if (entry.getName().endsWith(".log")) {
File outputFile = new File(destDir + File.separator + entry.getName());
File parentDir = outputFile.getParentFile();
boolean parentCreated = parentDir.mkdirs();
if (!parentCreated && !parentDir.exists()) {
logger.error("创建父目录失败:{} ", parentDir);
}
try (OutputStream output = Files.newOutputStream(outputFile.toPath())) {
int bytesRead;
while ((bytesRead = tarStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
}
}
}
}
}
/**
* 在查找文件中的数据
*
* @param result 结果
* @param dir dir
* @throws IOException ioexception
*/
private static void processFilesInDir(StringBuilder result, File dir) throws IOException {
for (File file : Objects.requireNonNull(dir.listFiles())) {
if (file.isDirectory()) {
processFilesInDir(result, file);
} else if (file.getName().endsWith(".log")) {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("Task")) {
System.out.println("Found line with 'Task': " + line);
result.append(line).append(" ");
}
}
reader.close();
}
}
}