Unzip files in swift

后端 未结 5 737
一整个雨季
一整个雨季 2020-12-16 04:22

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

相关标签:
5条回答
  • 2020-12-16 04:48

    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)

    0 讨论(0)
  • 2020-12-16 04:51

    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)
    
    0 讨论(0)
  • 2020-12-16 04:52

    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

    0 讨论(0)
  • 2020-12-16 04:57

    @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.
    0 讨论(0)
  • 2020-12-16 04:59

    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

    0 讨论(0)
提交回复
热议问题