Last active
April 4, 2017 23:13
-
-
Save Jire/8caa8603ef4871c79bef387f667a509f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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