|
| 1 | +package org.magmaoffenburg.roboviz.util |
| 2 | + |
| 3 | +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream |
| 4 | +import org.apache.commons.compress.compressors.CompressorStreamFactory |
| 5 | +import java.io.BufferedInputStream |
| 6 | +import java.io.BufferedReader |
| 7 | +import java.io.File |
| 8 | +import java.io.FileInputStream |
| 9 | +import java.io.FileNotFoundException |
| 10 | +import java.io.InputStream |
| 11 | +import java.io.InputStreamReader |
| 12 | +import java.util.zip.ZipFile |
| 13 | +import kotlin.math.max |
| 14 | + |
| 15 | +fun createBufferedReader(file: File): BufferedReader { |
| 16 | + // Extract suffixes from filename to determine extraction method |
| 17 | + val suffixes = file.name.lowercase().split('.').let { it.subList(max(0, it.size - 2), it.size) }.map { |
| 18 | + // Fix a few common shortened suffixes |
| 19 | + when (it) { |
| 20 | + "tbz2" -> listOf("tar", "bz2") |
| 21 | + "tgz" -> listOf("tar", "gz") |
| 22 | + else -> listOf(it) |
| 23 | + } |
| 24 | + }.flatten() |
| 25 | + |
| 26 | + var inputStream: InputStream = BufferedInputStream(FileInputStream(file)) |
| 27 | + for (suffix in suffixes.asReversed()) { |
| 28 | + when (suffix) { |
| 29 | + "bz2" -> inputStream = |
| 30 | + CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.BZIP2, inputStream) |
| 31 | + |
| 32 | + "gz" -> inputStream = |
| 33 | + CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.GZIP, inputStream) |
| 34 | + |
| 35 | + "tar" -> { |
| 36 | + // Only works for the current layout of tar files |
| 37 | + val tarStream = TarArchiveInputStream(inputStream) |
| 38 | + var entry = tarStream.nextEntry |
| 39 | + |
| 40 | + // Choose first file when not able to navigate deeper in the directory structure |
| 41 | + while (entry != null && entry.isDirectory) { |
| 42 | + val entries = entry.directoryEntries |
| 43 | + entry = if (entries.isNotEmpty()) { |
| 44 | + entries[0] |
| 45 | + } else { |
| 46 | + // empty directory |
| 47 | + tarStream.nextEntry |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Search for proper file |
| 52 | + while (entry != null && !entry.name.endsWith("sparkmonitor.log")) { |
| 53 | + entry = tarStream.nextEntry |
| 54 | + } |
| 55 | + |
| 56 | + if (entry == null) { |
| 57 | + throw FileNotFoundException("tar file does not contain logfile") |
| 58 | + } |
| 59 | + |
| 60 | + // We have reached the proper position |
| 61 | + inputStream = tarStream |
| 62 | + break |
| 63 | + } |
| 64 | + |
| 65 | + "zip" -> { |
| 66 | + val zipFile = ZipFile(file) |
| 67 | + if (zipFile.size() != 1) { |
| 68 | + zipFile.close() |
| 69 | + throw UnsupportedOperationException("Only support single entry zip files") |
| 70 | + } |
| 71 | + val zipEntry = zipFile.entries().nextElement() |
| 72 | + inputStream = zipFile.getInputStream(zipEntry) |
| 73 | + break |
| 74 | + } |
| 75 | + |
| 76 | + else -> break |
| 77 | + } |
| 78 | + } |
| 79 | + return BufferedReader(InputStreamReader(inputStream)) |
| 80 | +} |
0 commit comments