Thank you all for your insightful comments and suggestions regarding the issue I am facing with my Spring Boot application.
After reviewing the feedback, it seems that the root cause of the problem lies within the mock service that is generating a tar archive of the logs instead of a single log file. This explains why I was encountering a GZIP file containing another GZIP file when I attempted to save the logs.
To address this, I have modified my code to first extract the log files from the tar archive before attempting to decompress them. Here’s the updated code snippet that implements this change:
// Check if the fetched log data is valid
if (logData == null || logData.length == 0) {
model.addAttribute("errorMessage", "No log data retrieved. Please check the selected log files.");
return "downloaded_sgtw"; // Redirect to a result page with an error
}
// Specify the directory where you want to save the file
String destinationDir = "C:\\sgtw\\downInternal";
// Extract the log files from the tar archive
try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(new ByteArrayInputStream(logData))) {
TarArchiveEntry entry;
while ((entry = tarInputStream.getNextEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
String fileName = entry.getName();
String destinationPath = Paths.get(destinationDir, fileName).toString();
try (FileOutputStream fileOutputStream = new FileOutputStream(destinationPath)) {
IOUtils.copy(tarInputStream, fileOutputStream);
}
successfullyDownloadedFiles.add(fileName);
}
} catch (IOException e) {
e.printStackTrace();
model.addAttribute("errorMessage", "Error occurred while downloading logs: " + e.getMessage());
}
This code effectively extracts each log file from the tar archive and saves it in the specified directory. I appreciate the suggestion to inspect the HTTP response, as this helped me understand that the data being returned was not in the expected format.
Thank you again for your assistance! If there are any further improvements or considerations to make, please let me know.