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];
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.
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.
Swift 2 (Update):
So it works for me without Errors:
include the library to your Project Swift-ObjC Bridge (xxxx-Bridge-Header.h)
#import "SSZipArchive.h"
link the Library "libz.tbd" - Part of iOS
(Project -> Build Phases -> Link Binary With Libraries -> +)
Ready to zip/unzip
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With