Working with ZIP/archives in Kotlin: ZIP-InputStream and ZipFile

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with ZIP Archives in Kotlin: Complete Guide

In modern Kotlin (JVM) development, there is often a need to pack or unpack files. The standard Java library provides powerful tools for working with the ZIP format through the java.util.zip package. In this article, we will thoroughly examine how to create, read, and modify ZIP archives using Kotlin and built-in JVM capabilities.



Main Classes for Working with ZIP

For working with ZIP archives in Kotlin, the following classes from java.util.zip are used:

  • ZipOutputStream — for creating new ZIP archives and adding files to them.
  • ZipInputStream — for sequential reading of archive contents (streaming mode).
  • ZipFile — for random access to archive entries.
  • ZipEntry — represents a single entry (file or directory) inside the archive.

All these classes are part of the standard JDK library, so no additional dependencies are required.



Creating a ZIP Archive

To create an archive, ZipOutputStream is used, which wraps FileOutputStream. Each file is added via ZipEntry.

import java.io.Fileimport java.io.FileInputStreamimport java.io.FileOutputStreamimport java.util.zip.ZipEntryimport java.util.zip.ZipOutputStream

fun createZipArchive(sourceDir: String, zipFilePath: String) { val dir = File(sourceDir) val files = dir.listFiles() ?: return ZipOutputStream(FileOutputStream(zipFilePath)).use { zos -> for (file in files) { if (file.isFile) { // Create an entry with the file name val entry = ZipEntry(file.name) zos.putNextEntry(entry) // Copy the file contents FileInputStream(file).use { fis -> fis.copyTo(zos) } zos.closeEntry() } } } println("Archive created: $zipFilePath")}

// Example callfun main() { createZipArchive("/path/to/source", "/path/to/archive.zip")}


Extracting a ZIP Archive

Extraction is performed via ZipInputStream. We read each entry and restore files to disk.

import java.io.Fileimport java.io.FileInputStreamimport java.io.FileOutputStreamimport java.util.zip.ZipInputStream

fun extractZipArchive(zipFilePath: String, outputDir: String) { val outputFolder = File(outputDir).also { it.mkdirs() } ZipInputStream(FileInputStream(zipFilePath)).use { zis -> var entry = zis.nextEntry while (entry != null) { val fileName = entry.name val outputFile = File(outputFolder, fileName) // Create parent directories if needed outputFile.parentFile?.mkdirs() // Extract only files (not directories) if (!entry.isDirectory) { FileOutputStream(outputFile).use { fos -> zis.copyTo(fos) } } entry = zis.nextEntry } } println("Archive extracted to: $outputDir")}

// Example callfun main() { extractZipArchive("/path/to/archive.zip", "/path/to/extract")}


Reading Archive Contents Without Extraction

To view files inside a ZIP without extracting, use ZipFile and iterate over entries().

import java.io.Fileimport java.util.zip.ZipFile

fun listZipContents(zipFilePath: String) { ZipFile(File(zipFilePath)).use { zipFile -> println("Archive contents: ${zipFile.name}") println("Total entries: ${

Recommendations