From 17e9689337253b0916b8f661851ba0278d920085 Mon Sep 17 00:00:00 2001 From: Roy Marmelstein Date: Sun, 17 Jan 2016 07:14:14 +0100 Subject: [PATCH] Adding advanced functions --- README.md | 35 +++++++++++++++++++++----- ZipTests/ZipTests.swift | 56 ++++++++++++++++++++++++++++++++--------- 2 files changed, 73 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 8cd8d3b..5319534 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) # 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 @@ -16,14 +16,37 @@ import Zip ## 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 do { - let bb8FilePath = NSBundle.mainBundle().URLForResource("bb8", withExtension: "zip")! - try Zip().quickUnzipFile(bb8FilePath) // Unzip - try Zip().quickZipFiles([bb8FilePath], fileName: "archive") // Zip + let filePath = NSBundle.mainBundle().URLForResource("file", withExtension: "zip")! + let unzipDirectory = try Zip().quickUnzipFile(filePath) // Unzip + 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") } ``` diff --git a/ZipTests/ZipTests.swift b/ZipTests/ZipTests.swift index 2e0c8d5..ee9f16a 100644 --- a/ZipTests/ZipTests.swift +++ b/ZipTests/ZipTests.swift @@ -21,8 +21,8 @@ class ZipTests: XCTestCase { func testQuickUnzip() { do { - let fileAbsoluteURL = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! - let destinationURL = try Zip().quickUnzipFile(fileAbsoluteURL) + let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! + let destinationURL = try Zip().quickUnzipFile(filePath) let fileManager = NSFileManager.defaultManager() XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!)) } @@ -34,8 +34,8 @@ class ZipTests: XCTestCase { func testQuickUnzipNonExistingPath() { do { let filePathURL = NSBundle(forClass: ZipTests.self).resourcePath - let fileAbsoluteURL = NSURL(string:"\(filePathURL!)/bb9.zip") - let destinationURL = try Zip().quickUnzipFile(fileAbsoluteURL!) + let filePath = NSURL(string:"\(filePathURL!)/bb9.zip") + let destinationURL = try Zip().quickUnzipFile(filePath!) let fileManager = NSFileManager.defaultManager() XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!)) } @@ -46,8 +46,8 @@ class ZipTests: XCTestCase { func testQuickUnzipNonZipPath() { do { - let fileAbsoluteURL = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")! - let destinationURL = try Zip().quickUnzipFile(fileAbsoluteURL) + let filePath = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")! + let destinationURL = try Zip().quickUnzipFile(filePath) let fileManager = NSFileManager.defaultManager() XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!)) } @@ -58,8 +58,8 @@ class ZipTests: XCTestCase { func testQuickUnzipProgress() { do { - let fileAbsoluteURL = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! - try Zip().quickUnzipFile(fileAbsoluteURL, progress: { (progress) -> () in + let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! + try Zip().quickUnzipFile(filePath, progress: { (progress) -> () in XCTAssert(true) }) } @@ -68,10 +68,10 @@ class ZipTests: XCTestCase { } } - func testUnzipOnlineURL() { + func testQuickUnzipOnlineURL() { do { - let fileAbsoluteURL = NSURL(string: "http://www.google.com/google.zip")! - let destinationURL = try Zip().quickUnzipFile(fileAbsoluteURL) + let filePath = NSURL(string: "http://www.google.com/google.zip")! + let destinationURL = try Zip().quickUnzipFile(filePath) let fileManager = NSFileManager.defaultManager() XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!)) } @@ -79,7 +79,21 @@ class ZipTests: XCTestCase { XCTAssert(true) } } - + + 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() { do { @@ -93,5 +107,23 @@ class ZipTests: XCTestCase { XCTFail() } } + + 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() + } + } + } -- 2.26.2