public fun doExtractTeamCity()

in src/main/kotlin/org/jetbrains/teamcity/maven/sdk/Utils.kt [115:161]


    public fun doExtractTeamCity(archive: File, destination: File): File {
        logInfo("Unpacking TeamCity to ${destination.absolutePath}")

        destination.mkdirs()
        val counterFlag = AtomicBoolean(true)
        val unpackingCounter = CountingInputStream(FileInputStream(archive))
        val unpackingCounterThread = createCounter(counterFlag, unpackingCounter)

        val tarInput = TarArchiveInputStream(GZIPInputStream(BufferedInputStream(unpackingCounter)))
        val tarChannel = Channels.newChannel(tarInput)!!
        try {
            unpackingCounterThread.start()
            tarInput.eachEntry {
                val name: String
                val entryName = it.name
                if (entryName.startsWith("TeamCity")) {
                    name = entryName.substring("TeamCity".length)
                } else {
                    name = entryName
                }
                val destPath = File(destination, name)
                if (it.isDirectory) {
                    logDebug("Creating dir ${destPath.absolutePath}")
                    destPath.mkdirs()
                } else {
                    logDebug("Creating dir ${destPath.parentFile?.absolutePath}")
                    destPath.parentFile?.mkdirs()
                    logDebug("Creating file ${destPath.absolutePath}")
                    destPath.createNewFile()
                    val destOS = FileOutputStream(destPath)
                    try {
                        destOS.channel.transferFrom(tarChannel, 0, it.size)
                        if (it.isExecutable()) {
                            destPath.setExecutable(true)
                        }
                    } finally {
                        destOS.close()
                    }
                }
            }
        } finally {
            counterFlag.set(false)
            unpackingCounterThread.join(1000)
            tarChannel.close()
        }
        return destination
    }