Commit 26f376da authored by Roy Marmelstein's avatar Roy Marmelstein

Defining quick zip functions

parent bb7c238e
......@@ -36,6 +36,7 @@
3430F6341C45C851007473A6 /* unzip.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F62A1C45C851007473A6 /* unzip.h */; };
3430F6351C45C851007473A6 /* zip.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430F62B1C45C851007473A6 /* zip.c */; };
3430F6361C45C851007473A6 /* zip.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F62C1C45C851007473A6 /* zip.h */; };
3443A3F61C4AB8A3004AD173 /* QuickZip.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3443A3F51C4AB8A3004AD173 /* QuickZip.swift */; };
347E3A781C1DFFB500A11FD3 /* Zip.h in Headers */ = {isa = PBXBuildFile; fileRef = 347E3A771C1DFFB500A11FD3 /* Zip.h */; settings = {ATTRIBUTES = (Public, ); }; };
347E3A7F1C1DFFB500A11FD3 /* Zip.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 347E3A741C1DFFB500A11FD3 /* Zip.framework */; };
347E3A841C1DFFB500A11FD3 /* ZipTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 347E3A831C1DFFB500A11FD3 /* ZipTests.swift */; };
......@@ -83,6 +84,7 @@
3430F62B1C45C851007473A6 /* zip.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = zip.c; sourceTree = "<group>"; };
3430F62C1C45C851007473A6 /* zip.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = zip.h; sourceTree = "<group>"; };
3433BFCD1C48D3EC006DF000 /* module.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
3443A3F51C4AB8A3004AD173 /* QuickZip.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QuickZip.swift; sourceTree = "<group>"; };
347E3A741C1DFFB500A11FD3 /* Zip.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Zip.framework; sourceTree = BUILT_PRODUCTS_DIR; };
347E3A771C1DFFB500A11FD3 /* Zip.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Zip.h; sourceTree = "<group>"; };
347E3A791C1DFFB500A11FD3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
......@@ -182,6 +184,7 @@
347E3A771C1DFFB500A11FD3 /* Zip.h */,
347E3A791C1DFFB500A11FD3 /* Info.plist */,
347E3AD71C1E04C900A11FD3 /* Zip.swift */,
3443A3F51C4AB8A3004AD173 /* QuickZip.swift */,
);
path = Zip;
sourceTree = "<group>";
......@@ -330,6 +333,7 @@
3430BB2F1C484A65001143B5 /* entropy.c in Sources */,
3430F6351C45C851007473A6 /* zip.c in Sources */,
3430BB281C484A65001143B5 /* aescrypt.c in Sources */,
3443A3F61C4AB8A3004AD173 /* QuickZip.swift in Sources */,
3430BB311C484A65001143B5 /* fileenc.c in Sources */,
3430BB381C484A65001143B5 /* pwd2key.c in Sources */,
);
......
//
// QuickZip.swift
// Zip
//
// Created by Roy Marmelstein on 16/01/2016.
// Copyright © 2016 Roy Marmelstein. All rights reserved.
//
import Foundation
extension Zip {
//MARK: Quick Unzip
/**
Quick unzip a file. Unzips to a new folder inside the app's documents folder with the zip file's name.
- parameter path: Path of zipped file. NSURL.
- throws: Error if unzipping fails or if file is not found. Can be printed with a description variable.
- returns: NSURL of the destination folder.
*/
public func quickUnzipFile(path: NSURL) throws -> NSURL {
return try quickUnzipFile(path, progress: nil)
}
/**
Quick unzip a file. Unzips to a new folder inside the app's documents folder with the zip file's name.
- parameter path: Path of zipped file. NSURL.
- parameter progress: A progress closure called after unzipping each file in the archive. Double value betweem 0 and 1.
- throws: Error if unzipping fails or if file is not found. Can be printed with a description variable.
- returns: NSURL of the destination folder.
*/
public func quickUnzipFile(path: NSURL, progress: ((progress: Double) -> ())?) throws -> NSURL {
guard let fileExtension = path.pathExtension, let fileName = path.lastPathComponent else {
throw ZipError.UnzipError
}
let directoryName = fileName.stringByReplacingOccurrencesOfString(fileExtension, withString: "")
let destinationUrl = documentsUrl.URLByAppendingPathComponent(directoryName, isDirectory: true)
try self.unzipFile(path, destination: destinationUrl, overwrite: true, password: nil, progress: progress)
return destinationUrl
}
//MARK: Quick Zip
/**
Quick zip files.
- parameter paths: Array of NSURL filepaths.
- parameter fileName: File name for the resulting zip file.
- throws: rror if zipping fails.
- returns: NSURL of the destination folder.
*/
public func quickZipFiles(paths: [NSURL], fileName: String) throws -> NSURL {
return try quickZipFiles(paths, fileName: fileName, progress: nil)
}
/**
Quick zip files.
- parameter paths: Array of NSURL filepaths.
- parameter fileName: File name for the resulting zip file.
- parameter progress: A progress closure called after unzipping each file in the archive. Double value betweem 0 and 1.
- throws: rror if zipping fails.
- returns: NSURL of the destination folder.
*/
public func quickZipFiles(paths: [NSURL], fileName: String, progress: ((progress: Double) -> ())?) throws -> NSURL {
let destinationUrl = documentsUrl.URLByAppendingPathComponent("\(fileName).zip")
try self.zipFiles(paths, destination: destinationUrl, password: nil, progress: progress)
return destinationUrl
}
}
\ No newline at end of file
......@@ -14,6 +14,7 @@ public enum ZipError: ErrorType {
case FileNotFound // File not found
case UnzipError // Unzip error
case ZipError // Unzip error
case NotAZipFileError // Unzip error
/// Description variable
public var description: String {
......@@ -21,6 +22,7 @@ public enum ZipError: ErrorType {
case .FileNotFound: return NSLocalizedString("File not found.", comment: "")
case .UnzipError: return NSLocalizedString("Failed to unzip zip file.", comment: "")
case .ZipError: return NSLocalizedString("Failed to zip file.", comment: "")
case .NotAZipFileError: return NSLocalizedString("The file path does not contain a zip file.", comment: "")
}
}
}
......@@ -28,6 +30,9 @@ public enum ZipError: ErrorType {
public class Zip {
// Documents folder
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
// MARK: Lifecycle
/**
......@@ -40,19 +45,6 @@ public class Zip {
// MARK: Unzip
/**
Quick unzip file. Unzips to the app's documents folder.
- parameter path: Path of zipped file. NSURL.
- throws: Error if unzipping fails or if fail is not found. Can be printed with a description variable.
*/
public func unzipFile(path: NSURL) throws {
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
try self.unzipFile(path, destination: documentsUrl, overwrite: true, password: nil, progress:nil)
}
/**
Unzip file
......@@ -60,6 +52,7 @@ public class Zip {
- parameter destination: Path to unzip to. NSURL.
- parameter overwrite: Overwrite bool.
- parameter password: Optional password if file is protected.
- parameter progress: A progress closure called after unzipping each file in the archive. Double value betweem 0 and 1.
- throws: Error if unzipping fails or if fail is not found. Can be printed with a description variable.
*/
......@@ -166,26 +159,13 @@ public class Zip {
// MARK: Zip
/**
Quick zip files.
- parameter paths: Array of NSURL filepaths.
- parameter fileName: File name for the resulting zip file.
- throws: rror if zipping fails.
*/
public func zipFiles(paths: [NSURL], fileName: String) throws {
var documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
documentsUrl = documentsUrl.URLByAppendingPathComponent("\(fileName).zip")
try self.zipFiles(paths, destination: documentsUrl, password: nil, progress: nil)
}
/**
Zip files.
- parameter paths: Array of NSURL filepaths.
- parameter destination: Destination NSURL, should lead to a .zip filepath.
- parameter password: Password string. Optional.
- parameter progress: A progress closure called after unzipping each file in the archive. Double value betweem 0 and 1.
- throws: Error if zipping fails.
*/
......
......@@ -18,8 +18,8 @@ class ViewController: UIViewController {
let fileAbsolutePath = NSBundle.mainBundle().pathForResource("master", ofType: "zip")
let fileAbsoluteURL = NSURL(string: fileAbsolutePath!)!
print(destinationPath)
try Zip().unzipFile(fileAbsoluteURL)
try Zip().zipFiles([fileAbsoluteURL], fileName: "zipTest")
try Zip().quickUnzipFile(fileAbsoluteURL)
try Zip().quickZipFiles([fileAbsoluteURL], fileName: "zipTest")
}
catch {
......
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