Swift uses ssziparchive to implement file compression, decompression code

Source: Internet
Author: User
Tags create zip create zip file extract zip file zip ziparchive password protection

In order to save traffic, we need to package them into zip files for transmission, or unzip the downloaded zip package. This article describes how to use ziparchive for file compression, decompression operations.

1,ssziparchive Introduction

Ssziparchive is a use of objective-c written in the iOS, Mac compression, decompression tool class.
GitHub Address: Https://github.com/ZipArchive/ZipArchive
Features are as follows:

(1) Extract zip file
(2) Unzip the zip file with password protection
(3) Create zip file
(4) Add new file to zip file
(5) Compressed file
(6) using a name to compress the NSData object

Installation configuration for 2,ssziparchive

(1) Add the downloaded Ssziparchive folder to the project


(2) Create the bridge header file Bridge.h to contain the OBJECTIVE-C header files that need to be referenced as follows:

#import "ZipArchive.h"

(3) Click the plus sign in the project target-> build phases-> Link Binary with libraries, add libz.dylib

3, using the sample

First, in order to facilitate the next test, we first add two pictures in the project, as well as two compressed package files (where Test_password.zip is a password-packed package, the password is: hangge.com)

Also define a method to return the target path (each call creates a random folder under the program's caches) so that each compressed, uncompressed target save address does not conflict:


Randomly create a folder under the Caches folder and return to the path
Func Tempdestpath ()-> String? {
var path = Nssearchpathfordirectoriesindomains (. Cachesdirectory,
. Userdomainmask, True) [0]
Path + = "/\ (Nsuuid (). uuidstring) "
Let URL = Nsurl (Fileurlwithpath:path)

do {
Try Nsfilemanager.defaultmanager (). Createdirectoryaturl (URL,
Withintermediatedirectories:true, Attributes:nil)
catch {
return Nil
}

If let Path = Url.path {
Print ("Path:\ (Path)")
return path
}

return Nil
}

(1) Extract the normal zip file


Let Zippath = Nsbundle.mainbundle (). Pathforresource ("Test", OfType: "Zip")
Ssziparchive.unzipfileatpath (Zippath, Todestination:tempdestpath ())

(2) Unzip the zip file with the password


Let zipPath2 = Nsbundle.mainbundle (). Pathforresource ("Test_password", OfType: "Zip")

do {
Try Ssziparchive.unzipfileatpath (ZipPath2, Todestination:tempdestpath (),
Overwrite:true, Password: "hangge.com")
catch {
}


(3) Make the file into a compressed package


let files = [Nsbundle.mainbundle (). Pathforresource ("logo", OfType: "PNG")!,
Nsbundle.mainbundle (). Pathforresource ("icon", OfType: "PNG")!]

Let ZipPath3 = Tempdestpath ()! + "/hangge.zip"

Ssziparchive.createzipfileatpath (ZipPath3, Withfilesatpaths:files)

Of course, we can also add a password to the compressed package:

Ssziparchive.createzipfileatpath (ZipPath3, Withfilesatpaths:files,
Withpassword: "hangge.com")

(4) The file under the entire folder into a compressed package


Folders that need to be compressed.
Let filepath:string = nshomedirectory () + "/documents"
Add a file to the folder first
Let image = UIImage (named: "Logo.png")
Let Data:nsdata = Uiimagepngrepresentation (image!)!
Data.writetofile (FilePath + "/logo.png", Atomically:true)


Let ZipPath5 = Tempdestpath ()! + "/hangge.zip"
Ssziparchive.createzipfileatpath (ZipPath5, Withcontentsofdirectory:filepath)
Similarly, we can also add a password:

Ssziparchive.createzipfileatpath (ZipPath6, Withcontentsofdirectory:filepath,
Withpassword: "hangge.com")//with password

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.