Skip to content

Commit

Permalink
re-add tar handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxcapades committed Nov 22, 2024
1 parent 7e467b9 commit 824d289
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/main/kotlin/org/veupathdb/vdi/lib/common/compression/Tar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.veupathdb.vdi.lib.common.compression

import org.apache.commons.compress.archivers.tar.TarArchiveEntry
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream
import java.nio.file.Path
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
import kotlin.io.path.*

object Tar {
fun compressWithGZip(output: Path, inputs: Collection<Path>) {
if (output.exists())
throw IllegalStateException("target output path already exists!")

TarArchiveOutputStream(GZIPOutputStream(output.outputStream().buffered())).use { tar ->
inputs.forEach { file ->
tar.putArchiveEntry(TarArchiveEntry(file, file.name))
file.inputStream().use { inp -> inp.transferTo(tar) }
tar.closeArchiveEntry()
}
}
}

fun decompressWithGZip(inputFile: Path, outputDir: Path) {
if (!outputDir.exists())
outputDir.createDirectories()
if (!outputDir.isDirectory())
throw IllegalStateException("target output path does not point to a directory")

TarArchiveInputStream(GZIPInputStream(inputFile.inputStream().buffered())).use { tar ->
tar.forEach { entry ->
val target = outputDir.resolve(entry.name)

if (entry.isDirectory) {
target.createDirectories()
} else if (entry.isFile) {
if (target.exists()) {
if (target.isDirectory())
throw IllegalStateException("unable to unpack the file $target due to a directory already existing at that path")
else
throw IllegalStateException("unable to unpack the file $target due to a conflicting file already existing at that path")
}

target.parent.createDirectories()
target.createFile()
target.outputStream().use { os -> tar.transferTo(os) }
}
}
}
}

private inline fun TarArchiveInputStream.forEach(fn: (entry: TarArchiveEntry) -> Unit) {
while (true)
fn(nextEntry ?: break)
}
}

0 comments on commit 824d289

Please sign in to comment.