Decompress a zip file with Swift

I just find the following library called Minizip. Try it

You can use Zip framework to unZip your file using Swift. Simply written in Swift

Sample Code

do {
    let filePath = NSBundle.mainBundle().URLForResource("file", withExtension: "zip")!
    let unzipDirectory = try Zip.quickUnzipFile(filePath) // Unzip
    let zipFilePath = try Zip.quickZipFiles([filePath], fileName: "archive") // Zip
}
catch {
  print("Something went wrong")
}

If not works for minizip, you can go with ZipArchive, its not written in swift but in Objective-C


I recently released a Swift native framework that allows you to create, read and update ZIP archive files: ZIP Foundation.
It internally uses libcompression for great compression performance.

Unzipping a file is basically just one line:

try fileManager.unzipItem(at: sourceURL, to: destinationURL)

A full example with some context would look like this:

let fileManager = FileManager()
let currentWorkingPath = fileManager.currentDirectoryPath
var sourceURL = URL(fileURLWithPath: currentWorkingPath)
sourceURL.appendPathComponent("archive.zip")
var destinationURL = URL(fileURLWithPath: currentWorkingPath)
destinationURL.appendPathComponent("directory")
do {
    try fileManager.createDirectory(at: destinationURL, withIntermediateDirectories: true, attributes: nil)
    try fileManager.unzipItem(at: sourceURL, to: destinationURL)
} catch {
    print("Extraction of ZIP archive failed with error:\(error)")
}

The README on GitHub contains more information. All public methods also have full documentation available via Xcode Quick Help.

I also wrote a blog post about the performance characteristics here.


I got it work with SSZipArchive and a bridging header:

  1. Drag & drop the SSZipArchive directory in your project
  2. Create a {PROJECT-MODULE-NAME}-Bridging-Header.h file with the line import "SSZipArchive.h"
  3. In Build Settings, drag the bridging header in Swift Compiler - Code generation -> Objective-C Bridging Header.
  4. Use SSZipArchive in your Swift code.

Tags:

Ios

Zip

Swift