Skip to content

Instantly share code, notes, and snippets.

@Jire
Last active April 4, 2017 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jire/8caa8603ef4871c79bef387f667a509f to your computer and use it in GitHub Desktop.
Save Jire/8caa8603ef4871c79bef387f667a509f to your computer and use it in GitHub Desktop.
fun createTarGz(`in`: File, out: File) {
val fOut = FileOutputStream(out)
val bOut = BufferedOutputStream(fOut)
val gzOut = GzipCompressorOutputStream(bOut)
TarArchiveOutputStream(gzOut).use {
addFileToTarGz(it, `in`, "")
it.finish()
}
}
private fun addFileToTarGz(tOut: TarArchiveOutputStream, out: File, base: String) {
val entryName = base + out.name
val tarEntry = TarArchiveEntry(out, entryName)
tOut.putArchiveEntry(tarEntry)
if (out.isFile) {
FileInputStream(out).use {
IOUtils.copy(it, tOut)
tOut.closeArchiveEntry()
}
} else {
tOut.closeArchiveEntry()
out.listFiles()?.forEach {
addFileToTarGz(tOut, it, entryName + "/")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment