Working with ZIP/archives in Kotlin: a complete guide
In modern Kotlin development (especially for JVM and Android), there is often a need to work with ZIP archives: pack logs, unpack resources, create backups, or transfer data in compressed form. In this article, we will take a detailed look at how to work with ZIP archives in Kotlin using built-in Java capabilities (the java.util.zip package), as well as consider convenient extensions and alternatives.
Why work with archives in Kotlin?
- Saving space — compressing data when storing on disk.
- Convenience of transfer — one archive instead of many files.
- Security — the ability to encrypt (with additional libraries).
- Working with resources — unpacking configurations or updates.
Main Java classes for working with ZIP
Kotlin is fully compatible with Java, so for working with archives we use the following classes from java.util.zip:
ZipOutputStream— for creating an archive.ZipInputStream— for reading an archive.ZipEntry— represents an individual file/folder inside the archive.ZipFile— a convenient class for reading an existing archive.
Example 1: Creating a ZIP archive in Kotlin
Let's create an archive that contains several files and folders. We use ZipOutputStream and BufferedOutputStream for better performance.
import java.io.*import java.util.zip.*
fun createZipArchive( filesToZip: List<File>, outputZipPath: String) { ZipOutputStream(BufferedOutputStream(FileOutputStream(outputZipPath))).use { zos -> for (file in filesToZip) { if (file.isDirectory) { // Add an empty folder zos.putNextEntry(ZipEntry("${file.name}/")) zos.closeEntry() continue } // Add a file val entry = ZipEntry(file.name) zos.putNextEntry(entry) FileInputStream(file).use { fis -> fis.copyTo(zos) } zos.closeEntry() } } println("Archive created: $outputZipPath")}
fun main() { val files = listOf( File("document.txt"), File("image.png"), File("configs") // folder ) createZipArchive(files, "output.zip")}Example 2: Reading the contents of a ZIP archive
To view the list of files inside an archive, it is convenient to use ZipFile.
import java.util.zip.ZipFile
fun listZipContents(zipPath: String) { ZipFile(zipPath).use { zip -> println("Contents of archive $zipPath:") zip.entries().asSequence().forEach { entry -> val size = if (entry.isDirectory) "folder" else "${entry.size} bytes" println("- ${entry.name} ($size)") } }}
fun main() { listZipContents("output.zip")}Example 3: Extracting a ZIP archive
Let's extract all files from the archive to a specified folder. Pay attention to handling nested folders.
import java.io.*import java.util.zip.ZipInputStream
fun extractZip( zipPath: String, outputDir: String = "extracted") { val destDir = File(outputDir) destDir.mkdirs()
ZipInputStream(BufferedInputStream(FileInputStream(zipPath))).use { zis -> var entry = zis.nextEntry while (entry != null) { val file = File(destDir, entry.name) if (entry.isDirectory) { file.mkdirs() } else { // Create parent folders file.parentFile?.mkdirs() BufferedOutputStream(Fi Also in library
Working with Excel in JavaScript (Node.js): Libraries and Code Examples
Parsing HTML in PHP: Overview of Libraries and Tools
OAuth Client for TypeScript: Integrating Authorization in Web Applications
Sarama for Go: Complete Guide with Examples
StackExchange.Redis for C# (.NET): Complete Guide with Examples