Skip to content

ayanbabi90/SwiftZipKit

Repository files navigation

SwiftZipKit

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.

Overview

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.

Key Features

  • 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 zlib for compression and follows standard ZIP file structure (Local File Headers, Central Directory).

Technical Implementation

Architecture

The core logic is contained within the SwiftZipKit/Core module:

  • ZipCompression.swift: specific wrapper around the system zlib library. Handles raw deflate compression 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.

Streaming & Memory Management

The application avoids Data(contentsOf: url) for large files. Instead, it uses:

  • InputStream for reading source files.
  • FileHandle for writing to the destination.
  • A fixed 64KB buffer size for all read/write operations.

Concurrency

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.

Project Structure

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

Usage

Creating a ZIP

let files = [fileURL1, fileURL2]
let destination = resultURL

try ZipWriter.createZip(from: files, to: destination) { progress in
    print("Compression Progress: \(progress * 100)%")
}

Extracting a ZIP

let zipFile = sourceURL
let destination = extractDirectory

let reader = try ZipReader(file: zipFile)
try reader.extractAll(to: destination) { progress in
    print("Extraction Progress: \(progress * 100)%")
}

Requirements

  • iOS 15.0+ or macOS 12.0+
  • Swift 5.5+
  • Xcode 13.0+

License

This project is open-source and available for use in your applications.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages