SwiftZipKit is a high-performance, production-ready ZIP utility for iOS and macOS, designed to handle large files efficiently through streaming I/O and background processing.
This project provides a robust solution for creating and extracting ZIP archives. Unlike traditional in-memory ZIP handling, SwiftZipKit uses input streams and file handles to process data in small chunks (64KB). This allows it to handle multi-gigabyte files with minimal memory footprint, ensuring stability on resource-constrained devices.
- Streaming I/O: Compresses and creates ZIP files directly to disk without loading entire files into memory.
- Low Memory Footprint: Ideal for handling large datasets or high-resolution media.
- Background Processing: All heavy lifting (compression, decompression) is performed on background threads to keep the UI responsive.
- Progress Reporting: Real-time progress updates for both compression and extraction operations.
- File Preview: Preview individual files within a ZIP archive using QuickLook without extracting the entire archive.
- Standard Conformance: Uses system
zlibfor compression and follows standard ZIP file structure (Local File Headers, Central Directory).
The core logic is contained within the SwiftZipKit/Core module:
ZipCompression.swift: specific wrapper around the systemzliblibrary. Handles rawdeflatecompression and decompression in a streaming manner.ZipReader.swift: Parses ZIP files, locates the Central Directory, and allows for both full extraction and random access to individual entries.ZipWriter.swift: writes ZIP archives by streaming file data, calculating CRC32 checksums on the fly, and finalizing the archive with the Central Directory.ZipStructures.swift: Defines the binary structures (headers) used in the ZIP format.
The application avoids Data(contentsOf: url) for large files. Instead, it uses:
InputStreamfor reading source files.FileHandlefor writing to the destination.- A fixed 64KB buffer size for all read/write operations.
Operations are managed using Swift's concurrency model and Grand Central Dispatch (GCD). The ContentView uses state variables (isProcessing, progress) to update the UI based on callbacks from the background workers.
SwiftZipKit/
├── SwiftZipKit/
│ ├── ContentView.swift # Main UI with ProgressView and File processing logic
│ ├── PreviewController.swift # QuickLook preview integration
│ ├── Core/
│ │ ├── ZipCompression.swift
│ │ ├── ZipReader.swift
│ │ ├── ZipWriter.swift
│ │ ├── ZipStructures.swift
│ │ └── CRC32.swift
│ └── Assets.xcassets
└── README.md
let files = [fileURL1, fileURL2]
let destination = resultURL
try ZipWriter.createZip(from: files, to: destination) { progress in
print("Compression Progress: \(progress * 100)%")
}let zipFile = sourceURL
let destination = extractDirectory
let reader = try ZipReader(file: zipFile)
try reader.extractAll(to: destination) { progress in
print("Extraction Progress: \(progress * 100)%")
}- iOS 15.0+ or macOS 12.0+
- Swift 5.5+
- Xcode 13.0+
This project is open-source and available for use in your applications.