Working with ZIP/archives in Swift (iOS/macOS): a complete guide

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 Swift (iOS/macOS)



Data compression and extraction is one of the key tasks when developing applications for iOS and macOS. The ZIP format remains the most popular way to package files due to its versatility and built-in support in Apple operating systems. In this article, we will look at two main approaches to working with ZIP archives in Swift: using the built-in Foundation framework and the powerful third-party library Zip Foundation.



1. Built-in Foundation tools (NSFileCoordinator + libz)



Starting with iOS 9 and macOS 10.11, Apple provides limited but sufficient for basic tasks ZIP support through the NSFileCoordinator class and the system library libz. However, this method requires manual stream management and is not suitable for complex scenarios.



Example: extracting a ZIP archive using libz



import Foundationimport Compression

func unzipFile(at sourceURL: URL, to destinationURL: URL) throws { let sourceData = try Data(contentsOf: sourceURL) // Use the system decompressor let decodedData = try sourceData.withUnsafeBytes { (ptr: UnsafeRawBufferPointer) -> Data in let sourcePtr = ptr.baseAddress!.assumingMemoryBound(to: UInt8.self) let sourceSize = sourceData.count // Buffer for decompressed data (usually larger than the source) let bufferSize = sourceSize * 4 var buffer = Data(count: bufferSize) let decodedSize = buffer.withUnsafeMutableBytes { (destPtr: UnsafeMutableRawBufferPointer) -> Int in let dest = destPtr.baseAddress!.assumingMemoryBound(to: UInt8.self) return compression_decode_buffer(dest, bufferSize, sourcePtr, sourceSize, nil, COMPRESSION_ZLIB) } return buffer.prefix(decodedSize) } // Save the decompressed data try decodedData.write(to: destinationURL, options: .atomic)}


Limitations of the built-in approach:

  • No support for multi-file archives
  • No password handling
  • Requires manual buffer management
  • Difficult to handle large files


2. Zip Foundation library (recommended method)



Zip Foundation is an open-source library that provides a simple and elegant API for working with ZIP archives in Swift. It supports:

  • Creating and extracting archives
  • Working with passwords (AES-256)
  • Progress bars via Combine or closures
  • Streaming processing of large files
  • Support for macOS, iOS, watchOS, and tvOS


Installation via Swift Package Manager



// In Package.swift or via Xcode:dependencies: [    .package(url: "https://github.com/marmelroy/Zip.git", from: "2.1.0")]


Example: creating a ZIP archive



import Zip

func createZipArchive() { let fileManager = FileManager.default let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! // Collect files for archiving let file1 = documentsURL.appendingPathComponent("report.pdf") let file2 = documentsURL.appendingPathComponent("photo.jpg") let filesToZip = [file1, file2] // Path to the future archive let archiveURL = documentsURL.appendingPathComponent("backup.zip") do { // Progress via closure try Zip.zipFiles(paths: filesToZip, zipFilePath: archiveURL, password: nil, progress: { (progress) in print("Archiving progress: \\(Int(progress * 100))%") }) print("Archive created: \\(archiveURL.path)") } catch { print("Error creating archive: \\(error.localizedDescription)") }}


Example: extracting an archive with

Recommendations