Commit 17e96893 authored by Roy Marmelstein's avatar Roy Marmelstein

Adding advanced functions

parent 1d615e93
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
# Zip # Zip
A Swift framework for zipping and unzipping files. Simple and quick to use. A Swift framework for zipping and unzipping files. Simple and quick to use. Built on top of [nmoinvaz/minizip](https://github.com/nmoinvaz/minizip).
## Usage ## Usage
...@@ -16,14 +16,37 @@ import Zip ...@@ -16,14 +16,37 @@ import Zip
## Quick functions ## Quick functions
Zip includes two quick functions for zipping and unzipping files and they work as you would expect. Both functions take local file path urls. 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.
```swift ```swift
do { do {
let bb8FilePath = NSBundle.mainBundle().URLForResource("bb8", withExtension: "zip")! let filePath = NSBundle.mainBundle().URLForResource("file", withExtension: "zip")!
try Zip().quickUnzipFile(bb8FilePath) // Unzip let unzipDirectory = try Zip().quickUnzipFile(filePath) // Unzip
try Zip().quickZipFiles([bb8FilePath], fileName: "archive") // Zip let zipFilePath = try Zip().quickZipFiles([filePath], fileName: "archive") // Zip
} }
catch ErrorType { catch {
print("Something went wrong")
}
```
## Advanced Zip
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 function throw if there is an error but don't return.
```swift
do {
let filePath = NSBundle.mainBundle().URLForResource("file", withExtension: "zip")!
let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
try Zip().unzipFile(filePath, destination: documentsDirectory, overwrite: true, password: "password", progress: { (progress) -> () in
print(progress)
}) // Unzip
let zipFilePath = documentsFolder.URLByAppendingPathComponent("archive.zip")
try Zip().zipFiles([filePath], zipFilePath: zipFilePath, password: "password", progress: { (progress) -> () in
print(progress)
}) //Zip
}
catch {
print("Something went wrong") print("Something went wrong")
} }
``` ```
......
...@@ -21,8 +21,8 @@ class ZipTests: XCTestCase { ...@@ -21,8 +21,8 @@ class ZipTests: XCTestCase {
func testQuickUnzip() { func testQuickUnzip() {
do { do {
let fileAbsoluteURL = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")!
let destinationURL = try Zip().quickUnzipFile(fileAbsoluteURL) let destinationURL = try Zip().quickUnzipFile(filePath)
let fileManager = NSFileManager.defaultManager() let fileManager = NSFileManager.defaultManager()
XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!)) XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!))
} }
...@@ -34,8 +34,8 @@ class ZipTests: XCTestCase { ...@@ -34,8 +34,8 @@ class ZipTests: XCTestCase {
func testQuickUnzipNonExistingPath() { func testQuickUnzipNonExistingPath() {
do { do {
let filePathURL = NSBundle(forClass: ZipTests.self).resourcePath let filePathURL = NSBundle(forClass: ZipTests.self).resourcePath
let fileAbsoluteURL = NSURL(string:"\(filePathURL!)/bb9.zip") let filePath = NSURL(string:"\(filePathURL!)/bb9.zip")
let destinationURL = try Zip().quickUnzipFile(fileAbsoluteURL!) let destinationURL = try Zip().quickUnzipFile(filePath!)
let fileManager = NSFileManager.defaultManager() let fileManager = NSFileManager.defaultManager()
XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!)) XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!))
} }
...@@ -46,8 +46,8 @@ class ZipTests: XCTestCase { ...@@ -46,8 +46,8 @@ class ZipTests: XCTestCase {
func testQuickUnzipNonZipPath() { func testQuickUnzipNonZipPath() {
do { do {
let fileAbsoluteURL = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")! let filePath = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")!
let destinationURL = try Zip().quickUnzipFile(fileAbsoluteURL) let destinationURL = try Zip().quickUnzipFile(filePath)
let fileManager = NSFileManager.defaultManager() let fileManager = NSFileManager.defaultManager()
XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!)) XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!))
} }
...@@ -58,8 +58,8 @@ class ZipTests: XCTestCase { ...@@ -58,8 +58,8 @@ class ZipTests: XCTestCase {
func testQuickUnzipProgress() { func testQuickUnzipProgress() {
do { do {
let fileAbsoluteURL = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")!
try Zip().quickUnzipFile(fileAbsoluteURL, progress: { (progress) -> () in try Zip().quickUnzipFile(filePath, progress: { (progress) -> () in
XCTAssert(true) XCTAssert(true)
}) })
} }
...@@ -68,10 +68,10 @@ class ZipTests: XCTestCase { ...@@ -68,10 +68,10 @@ class ZipTests: XCTestCase {
} }
} }
func testUnzipOnlineURL() { func testQuickUnzipOnlineURL() {
do { do {
let fileAbsoluteURL = NSURL(string: "http://www.google.com/google.zip")! let filePath = NSURL(string: "http://www.google.com/google.zip")!
let destinationURL = try Zip().quickUnzipFile(fileAbsoluteURL) let destinationURL = try Zip().quickUnzipFile(filePath)
let fileManager = NSFileManager.defaultManager() let fileManager = NSFileManager.defaultManager()
XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!)) XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!))
} }
...@@ -80,6 +80,20 @@ class ZipTests: XCTestCase { ...@@ -80,6 +80,20 @@ class ZipTests: XCTestCase {
} }
} }
func testUnzip() {
do {
let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")!
let documentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
try Zip().unzipFile(filePath, destination: documentsFolder, overwrite: true, password: "password", progress: { (progress) -> () in
print(progress)
})
let fileManager = NSFileManager.defaultManager()
XCTAssertTrue(fileManager.fileExistsAtPath(documentsFolder.path!))
}
catch {
XCTFail()
}
}
func testQuickZip() { func testQuickZip() {
do { do {
...@@ -94,4 +108,22 @@ class ZipTests: XCTestCase { ...@@ -94,4 +108,22 @@ class ZipTests: XCTestCase {
} }
} }
func testZip() {
do {
let imageURL1 = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")!
let imageURL2 = NSBundle(forClass: ZipTests.self).URLForResource("kYkLkPf", withExtension: "gif")!
let documentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
let zipFilePath = documentsFolder.URLByAppendingPathComponent("archive.zip")
try Zip().zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath, password: nil, progress: { (progress) -> () in
print(progress)
})
let fileManager = NSFileManager.defaultManager()
XCTAssertTrue(fileManager.fileExistsAtPath(zipFilePath.path!))
}
catch {
XCTFail()
}
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment