Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unzip files in swift

How to go about unzipping a file in swift? In Objective-C, I used SSZipArchive and I loved it. As seen in the code below. I suspect if I decide to keep SSZipArchive, I will have to bridge an Objective-C file to my Swift file. Is there any updated third-party, or better yet Apple-Documentation to unzip a file in Swift?

NSString *zipPath = [self.globalFileStrucure stringByAppendingPathComponent:@"zipfile.zip"];

[data writeToFile:zipPath options:0 error:&error];

BOOL unZipped = 0;

unZipped = [SSZipArchive unzipFileAtPath:zipPath toDestination:self.globalFileStrucure];
like image 562
imnilly Avatar asked Oct 30 '14 13:10

imnilly


People also ask

How do I unZip a file in IOS 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.

How do I open a zip file in Xcode?

Importing into 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.


Video Answer


3 Answers

Swift 2 (Update):

So it works for me without Errors:

  1. download/clone the Library here: ssziparchive
  2. copy the Directory "SSZipArchive" to your Project (Drag&Drop) and select 'Create groups'
  3. include the library to your Project Swift-ObjC Bridge (xxxx-Bridge-Header.h)

    #import "SSZipArchive.h"

  4. link the Library "libz.tbd" - Part of iOS

    (Project -> Build Phases -> Link Binary With Libraries -> +)

  5. Ready to zip/unzip

like image 65
Peter Kreinz Avatar answered Oct 13 '22 00:10

Peter Kreinz


If you're using any of UIKit or AppKit, you're already working with a Swift-ObjC bridge. Don't worry about that, just use the library you know and love!

let zipPath = globalFileStrucure.stringByAppendingPathComponent("zipfile.zip")
data.writeToFile(zipPath, options: nil, error: &error)

let unZipped = SSZipArchive.unzipFileAtPath(zipPath, toDestination: globalFileStrucure)
like image 25
Nate Cook Avatar answered Oct 13 '22 00:10

Nate Cook


I found WPZipArchive is more easy to use and install, using Cocoapods. Anyone who interested can read the guide below:

1) Open Terminal.app from your Mac Application/Utilities folder or from your Launchpad

2) Enter sudo gem update --system into your Terminal to ensure your Ruby is up to date

3) Enter sudo gem install cocoapods to install cocoapods (This process will likely take a few minutes, just wait it show Completed)

4) Enter pod setup to setup the cocoapods (from here onward, you do not need to use sudo command, use only pod)

5) Launch Xcode and create a new project (If you do not have existing project created). Skip this step if you had a Xcode project created.

6) QUIT Xcode program. Yes, close your project and quit the Xcode program

7) Back to Terminal and enter cd Path/To/Folder/Containing/YourProject replace this Path/To/Folder/Containing/YourProject with your own directory path

8) Enter pod init

9) Enter open -a Xcode Podfile to open the podfile using Xcode program. The default Podfile looks like this:

platform :osx, '10.10'
use_frameworks!

target 'myprojectname' do
pod 'WPZipArchive', '0.0.1'
end

platform :osx, '10.10' is specify the minimum deployment target, you can change to whatever you preferred.

10) Save and Close Podfile (Close Xcode too)

11) Back to Terminal and enter pod install this is to install the WPZipArchive, this will take few minutes.

12) You are done installing WPZipArchive.

Launch Xcode Project:

IMPORTANT: From now on, you must launch your Xcode project using the .xcworkspace NOT .xcodeproj

On the ViewController.swift or any .swift file you want to use zip or unzip method, just add import WPZipArchive like below:

import Cocoa
import WPZipArchive

To Zip a file, call method like this:

WPZipArchive.createZipFileAtPath(zipPath, withContentsOfDirectory: sampleDataPath)

To Unzip a file, call method like this:

WPZipArchive.unzipFileAtPath(zipPath, toDestination: unzipPath)

That's all and it work and easy to implement.

Enjoy zipping and unzipping

like image 1
David Evony Avatar answered Oct 13 '22 01:10

David Evony