问题
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];
回答1:
Swift 2 (Update):
So it works for me without Errors:
- download/clone the Library here: ssziparchive
- copy the Directory "SSZipArchive" to your Project (Drag&Drop) and select 'Create groups'
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
回答2:
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)
回答3:
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
回答4:
It works with following Peter Kreinz.
Pay attention on step2 as below:
Please select the 'Create groups' when copy the Directory "SSZipArchive" to your Project (Drag&Drop)
回答5:
@screenworker (Unome) asked about a rash of errors that all make reference to the libz framework. I ran into something similar. The link step produced errors concerning "symbols" such as "_crc32". All these symbols could be found in the libz.tbd stub.
The issue in this case is that I had mistakenly added the libz.tbd reference to the Test target, not the Target named for the project itself. This can be checked by going to the Build Phases display:
- In the Project Navigator (left panel) click on your project name
- In the editor, click on the "Build Phases" tab.
- In the Build Phases display, look for a list entitled Targets in a narrow column adjacent to the Project Navigator
- Click on the target that is the same as your project (e.g. myProject and not myProjectTest or myProjectUITest)
- If the arrow beside "Link Binary With Libraries" is clicked you should see the list of libraries - which should in include libz.
来源:https://stackoverflow.com/questions/26654194/unzip-files-in-swift