Comprimere e decomprimere file tar.gz con Kotlin

Mattepuffo's logo
Comprimere e decomprimere file tar.gz con Kotlin

Comprimere e decomprimere file tar.gz con Kotlin

Kotlin di default non ha classi per la compressione / decompressione dei file tar.gz.

Però possiamo usare Apache Commons Compress.

Se usate Maven:

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.9</version>
        </dependency>

Qui sotto un codice con le due funzioni, una per la compressione ed una per la decompressione:

import org.apache.commons.compress.archivers.ArchiveEntry
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 org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption

fun main(args: Array<String>) {
    val desktop = "C:\Users\Pc\Desktop"

    val pathOut = Paths.get(desktop + "\file.tar.gz")
    val pathInput = Paths.get(desktop + "\file.xlsx")
    val target = Paths.get(desktop + "\test_comp")

    try {
        compress(pathInput, pathOut)
    } catch (ex: IOException) {
        println("ERR: " + ex.message)
    }

    println("COMPRESSIONE OK")

    try {
        decompress(pathOut, target)
    } catch (ex: IOException) {
        println("ERR: " + ex.message)
    }

    println("DE-COMPRESSIONE OK")
}

private fun compress(input: Path, output: Path) {
    Files.newOutputStream(output).use { fOut ->
        BufferedOutputStream(fOut).use { buffOut ->
            GzipCompressorOutputStream(buffOut).use { gzOut ->
                TarArchiveOutputStream(gzOut).use { tOut ->
                    val tarEntry = TarArchiveEntry(input.toFile(), input.fileName.toString())
                    tOut.putArchiveEntry(tarEntry)
                    Files.copy(input, tOut)
                    tOut.closeArchiveEntry()
                    tOut.finish()
                }
            }
        }
    }
}

private fun decompress(input: Path, output: Path) {
    Files.newInputStream(input).use { fi ->
        BufferedInputStream(fi).use { bi ->
            GzipCompressorInputStream(bi).use { gzi ->
                TarArchiveInputStream(gzi).use { ti ->
                    var entry: ArchiveEntry
                    while (ti.nextEntry.also { entry = it } != null) {
                        val targetDirResolved = output.resolve(entry.name)
                        val normalizePath = targetDirResolved.normalize()
                        if (entry.isDirectory) {
                            Files.createDirectories(normalizePath)
                        } else {
                            val parent = normalizePath.parent
                            if (parent != null) {
                                if (Files.notExists(parent)) {
                                    Files.createDirectories(parent)
                                }
                            }
                            Files.copy(ti, normalizePath, StandardCopyOption.REPLACE_EXISTING)
                        }
                    }
                }
            }
        }
    }
}

Enjoy!


Condividi

Commentami!