Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decompress a zip file with Swift

Tags:

ios

swift

zip

I made a game in Swift but I need to download .zip files on my website, and use the content (images) in game. Actually, I download the .zip file and store it in documents, and I need to unzip the file into the same documents folder.

I tried marmelroy's Zip, iOS 9 Compression module and tidwall's DeflateSwift but none of these worked. It could be good if it was compatible with iOS 8.

like image 967
Snowy_1803 Avatar asked Jul 05 '16 11:07

Snowy_1803


People also ask

How do I unzip a file in Swift?

Using this library you can use this line: let unzipDirectory= try Zip. quickUnzipFile(filePath) //to unZip your folder. If you want more help , check the repository.

Can you decompress a ZIP file?

Decompressing a zipped file or folderFind the file you want to decompress, right-click it, and choose Extract All. In the dialog box that appears, to choose the destination for the decompressed files, click Browse.... You can also check the option Show extracted files when complete. Click Extract.

How do I open a ZIP file in Xcode?

Open Xcode and select Open Another Project or File, Open. Open the folder you unzipped from your Dropsource download and locate the file with “. xcodeproj” extension in the root directory. Select the file and click Open.


1 Answers

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.

like image 152
Thomas Zoechling Avatar answered Sep 24 '22 10:09

Thomas Zoechling