README.md 2.29 KB
Newer Older
Roy Marmelstein's avatar
Roy Marmelstein committed
1 2 3 4
![Zip - Zip and unzip files in Swift](https://cloud.githubusercontent.com/assets/889949/12374908/252373d0-bcac-11e5-8ece-6933aeae8222.png)

[![Build Status](https://travis-ci.org/marmelroy/Zip.svg?branch=master)](https://travis-ci.org/marmelroy/Zip) [![Version](http://img.shields.io/cocoapods/v/Zip.svg)](http://cocoapods.org/?q=Zip)

Roy Marmelstein's avatar
Roy Marmelstein committed
5
# Zip
Roy Marmelstein's avatar
Roy Marmelstein committed
6
A Swift framework for zipping and unzipping files. Simple and quick to use. Built on top of [minizip](https://github.com/nmoinvaz/minizip).
Roy Marmelstein's avatar
Roy Marmelstein committed
7 8 9 10 11 12 13 14 15

## Usage

Import Zip at the top of the Swift file.

```swift
import Zip
```

Roy Marmelstein's avatar
Roy Marmelstein committed
16 17
## Quick functions

Roy Marmelstein's avatar
Roy Marmelstein committed
18
The easiest way to use Zip is through quick functions. Both take local file paths as NSURLs, throw if an error is encountered and return an NSURL to the destination if successful.
Roy Marmelstein's avatar
Roy Marmelstein committed
19 20
```swift
do {
Roy Marmelstein's avatar
Roy Marmelstein committed
21
    let filePath = NSBundle.mainBundle().URLForResource("file", withExtension: "zip")!
22 23
    let unzipDirectory = try Zip.quickUnzipFile(filePath) // Unzip
    let zipFilePath = try Zip.quickZipFiles([filePath], fileName: "archive") // Zip
Roy Marmelstein's avatar
Roy Marmelstein committed
24
}
Roy Marmelstein's avatar
Roy Marmelstein committed
25 26 27 28 29 30 31
catch {
  print("Something went wrong")
}
```

## Advanced Zip

Jake Welton's avatar
Jake Welton committed
32
For more advanced usage, Zip has functions that let you set custom  destination paths, work with password protected zips and use a progress handling closure. These functions throw if there is an error but don't return.
Roy Marmelstein's avatar
Roy Marmelstein committed
33 34 35 36 37
```swift
do {
    let filePath = NSBundle.mainBundle().URLForResource("file", withExtension: "zip")!
    let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL

38
    try Zip.unzipFile(filePath, destination: documentsDirectory, overwrite: true, password: "password", progress: { (progress) -> () in
Roy Marmelstein's avatar
Roy Marmelstein committed
39 40 41 42
        print(progress)
    }) // Unzip

    let zipFilePath = documentsFolder.URLByAppendingPathComponent("archive.zip")
43
    try Zip.zipFiles([filePath], zipFilePath: zipFilePath, password: "password", progress: { (progress) -> () in
Roy Marmelstein's avatar
Roy Marmelstein committed
44 45 46 47 48
        print(progress)
    }) //Zip

}
catch {
Roy Marmelstein's avatar
Roy Marmelstein committed
49 50 51 52
  print("Something went wrong")
}
```

53 54
## Custom File Extensions

Antoine Cœur's avatar
Antoine Cœur committed
55
Zip supports '.zip' and '.cbz' files out of the box. To support additional zip-derivative file extensions:
56 57 58 59
```
Zip.addCustomFileExtension("file-extension-here")
```

Roy Marmelstein's avatar
Roy Marmelstein committed
60 61 62
### Setting up with [CocoaPods](http://cocoapods.org/?q=Zip)
```ruby
source 'https://github.com/CocoaPods/Specs.git'
Roy Marmelstein's avatar
Roy Marmelstein committed
63
pod 'Zip', '~> 0.5', :submodules => true
Roy Marmelstein's avatar
Roy Marmelstein committed
64
```