Commit b5a63a0c authored by Roy Marmelstein's avatar Roy Marmelstein

Adding zip and unzip functions

parent c31fd888
...@@ -42,9 +42,21 @@ ...@@ -42,9 +42,21 @@
<barButtonItem title="0 Selected" style="plain" id="dci-jS-Xlf"> <barButtonItem title="0 Selected" style="plain" id="dci-jS-Xlf">
<color key="tintColor" red="0.1333333333" green="0.1333333333" blue="0.1333333333" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> <color key="tintColor" red="0.1333333333" green="0.1333333333" blue="0.1333333333" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</barButtonItem> </barButtonItem>
<barButtonItem style="plain" systemItem="flexibleSpace" id="331-sz-9FL"/>
<barButtonItem title="Zip" id="1qo-CZ-Gah">
<connections>
<action selector="zipSelection:" destination="dpw-R4-j8a" id="ujU-jh-wca"/>
</connections>
</barButtonItem>
<barButtonItem width="42" style="plain" systemItem="fixedSpace" id="Sp9-gV-7ef"/>
<barButtonItem title="Unzip" id="AXE-aZ-PBA">
<connections>
<action selector="unzipSelection:" destination="dpw-R4-j8a" id="vuT-QM-DUX"/>
</connections>
</barButtonItem>
</items> </items>
</toolbar> </toolbar>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="vkM-Ih-ncL"> <tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" allowsMultipleSelection="YES" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="vkM-Ih-ncL">
<rect key="frame" x="0.0" y="64" width="600" height="492"/> <rect key="frame" x="0.0" y="64" width="600" height="492"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<connections> <connections>
...@@ -68,6 +80,8 @@ ...@@ -68,6 +80,8 @@
<connections> <connections>
<outlet property="selectionCounter" destination="dci-jS-Xlf" id="uVp-Mf-mdx"/> <outlet property="selectionCounter" destination="dci-jS-Xlf" id="uVp-Mf-mdx"/>
<outlet property="tableView" destination="vkM-Ih-ncL" id="Pw4-pv-J01"/> <outlet property="tableView" destination="vkM-Ih-ncL" id="Pw4-pv-J01"/>
<outlet property="unzipButton" destination="AXE-aZ-PBA" id="fqk-D1-e4c"/>
<outlet property="zipButton" destination="1qo-CZ-Gah" id="OSm-ha-Ig5"/>
</connections> </connections>
</viewController> </viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="pqG-S7-8Xl" userLabel="First Responder" sceneMemberID="firstResponder"/> <placeholder placeholderIdentifier="IBFirstResponder" id="pqG-S7-8Xl" userLabel="First Responder" sceneMemberID="firstResponder"/>
......
...@@ -7,52 +7,68 @@ ...@@ -7,52 +7,68 @@
// //
import UIKit import UIKit
import Zip
class FileBrowser: UIViewController { class FileBrowser: UIViewController, UITableViewDataSource, UITableViewDelegate {
// IBOutlets
@IBOutlet weak var tableView: UITableView! @IBOutlet weak var tableView: UITableView!
@IBOutlet weak var selectionCounter: UIBarButtonItem! @IBOutlet weak var selectionCounter: UIBarButtonItem!
@IBOutlet weak var zipButton: UIBarButtonItem!
@IBOutlet weak var unzipButton: UIBarButtonItem!
let fileManager = NSFileManager.defaultManager() let fileManager = NSFileManager.defaultManager()
var path: NSURL? { var path: NSURL? {
didSet { didSet {
if let filePath = path { updateFiles()
var tempFiles = [String]()
do {
self.title = filePath.lastPathComponent
tempFiles = try self.fileManager.contentsOfDirectoryAtPath(filePath.path!)
} catch {
if path == "/System" {
tempFiles = ["Library"]
}
if path == "/Library" {
tempFiles = ["Preferences"]
}
if path == "/var" {
tempFiles = ["mobile"]
}
if path == "/usr" {
tempFiles = ["lib", "libexec", "bin"]
}
}
self.files = tempFiles.sort(){$0 < $1}
}
} }
} }
var files = [String]() var files = [String]()
var selectedFiles = [String]()
//MARK: Lifecycle
override func viewDidLoad() { override func viewDidLoad() {
if self.path == nil { if self.path == nil {
let documentsUrl = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL let documentsUrl = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
self.path = documentsUrl self.path = documentsUrl
} }
updateSelection()
}
//MARK: File manager
func updateFiles() {
if let filePath = path {
var tempFiles = [String]()
do {
self.title = filePath.lastPathComponent
tempFiles = try self.fileManager.contentsOfDirectoryAtPath(filePath.path!)
} catch {
if path == "/System" {
tempFiles = ["Library"]
}
if path == "/Library" {
tempFiles = ["Preferences"]
}
if path == "/var" {
tempFiles = ["mobile"]
}
if path == "/usr" {
tempFiles = ["lib", "libexec", "bin"]
}
}
self.files = tempFiles.sort(){$0 < $1}
tableView.reloadData()
}
} }
//MARK: UITableView Data Source and Delegate
func numberOfSectionsInTableView(tableView: UITableView) -> Int { func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1 return 1
} }
...@@ -70,7 +86,9 @@ class FileBrowser: UIViewController { ...@@ -70,7 +86,9 @@ class FileBrowser: UIViewController {
guard let path = path else { guard let path = path else {
return cell return cell
} }
let newPath = path.URLByAppendingPathComponent(files[indexPath.row]).path! cell.selectionStyle = .None
let filePath = files[indexPath.row]
let newPath = path.URLByAppendingPathComponent(filePath).path!
var isDirectory: ObjCBool = false var isDirectory: ObjCBool = false
fileManager.fileExistsAtPath(newPath, isDirectory: &isDirectory) fileManager.fileExistsAtPath(newPath, isDirectory: &isDirectory)
cell.textLabel?.text = files[indexPath.row] cell.textLabel?.text = files[indexPath.row]
...@@ -80,12 +98,69 @@ class FileBrowser: UIViewController { ...@@ -80,12 +98,69 @@ class FileBrowser: UIViewController {
else { else {
cell.imageView?.image = UIImage(named: "File") cell.imageView?.image = UIImage(named: "File")
} }
cell.backgroundColor = (selectedFiles.contains(filePath)) ? UIColor(white: 0.9, alpha: 1.0):UIColor.whiteColor()
return cell return cell
} }
required init?(coder aDecoder: NSCoder) { func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
super.init(coder: aDecoder) let filePath = files[indexPath.row]
if let index = selectedFiles.indexOf(filePath) where selectedFiles.contains(filePath) {
selectedFiles.removeAtIndex(index)
}
else {
selectedFiles.append(filePath)
}
updateSelection()
}
func updateSelection() {
tableView.reloadData()
selectionCounter.title = "\(selectedFiles.count) Selected"
zipButton.enabled = (selectedFiles.count > 0)
if (selectedFiles.count == 1) {
let filePath = selectedFiles.first
let pathExtension = path!.URLByAppendingPathComponent(filePath!).pathExtension
if pathExtension == "zip" {
unzipButton.enabled = true
}
else {
unzipButton.enabled = false
}
}
else {
unzipButton.enabled = false
}
} }
//MARK: Actions
@IBAction func unzipSelection(sender: AnyObject) {
let filePath = selectedFiles.first
let pathURL = path!.URLByAppendingPathComponent(filePath!)
do {
try Zip().quickUnzipFile(pathURL)
self.selectedFiles.removeAll()
updateSelection()
updateFiles()
} catch {
print("ERROR")
}
}
@IBAction func zipSelection(sender: AnyObject) {
var urlPaths = [NSURL]()
for filePath in selectedFiles {
urlPaths.append(path!.URLByAppendingPathComponent(filePath))
}
do {
try Zip().quickZipFiles(urlPaths, fileName: "Archive")
self.selectedFiles.removeAll()
updateSelection()
updateFiles()
} catch {
print("ERROR")
}
}
} }
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