Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Z
Zip
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Jira
Jira
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
YongYue
Zip
Commits
b5a63a0c
Commit
b5a63a0c
authored
Jan 17, 2016
by
Roy Marmelstein
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding zip and unzip functions
parent
c31fd888
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
118 additions
and
29 deletions
+118
-29
examples/Sample/Sample/Base.lproj/Main.storyboard
examples/Sample/Sample/Base.lproj/Main.storyboard
+15
-1
examples/Sample/Sample/FileBrowser.swift
examples/Sample/Sample/FileBrowser.swift
+103
-28
No files found.
examples/Sample/Sample/Base.lproj/Main.storyboard
View file @
b5a63a0c
...
...
@@ -42,9 +42,21 @@
<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"
/>
</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>
</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"
/>
<color
key=
"backgroundColor"
white=
"1"
alpha=
"1"
colorSpace=
"calibratedWhite"
/>
<connections>
...
...
@@ -68,6 +80,8 @@
<connections>
<outlet
property=
"selectionCounter"
destination=
"dci-jS-Xlf"
id=
"uVp-Mf-mdx"
/>
<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>
</viewController>
<placeholder
placeholderIdentifier=
"IBFirstResponder"
id=
"pqG-S7-8Xl"
userLabel=
"First Responder"
sceneMemberID=
"firstResponder"
/>
...
...
examples/Sample/Sample/FileBrowser.swift
View file @
b5a63a0c
...
...
@@ -7,52 +7,68 @@
//
import
UIKit
import
Zip
class
FileBrowser
:
UIViewController
{
class
FileBrowser
:
UIViewController
,
UITableViewDataSource
,
UITableViewDelegate
{
// IBOutlets
@IBOutlet
weak
var
tableView
:
UITableView
!
@IBOutlet
weak
var
selectionCounter
:
UIBarButtonItem
!
@IBOutlet
weak
var
zipButton
:
UIBarButtonItem
!
@IBOutlet
weak
var
unzipButton
:
UIBarButtonItem
!
let
fileManager
=
NSFileManager
.
defaultManager
()
var
path
:
NSURL
?
{
didSet
{
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
}
}
updateFiles
()
}
}
var
files
=
[
String
]()
var
selectedFiles
=
[
String
]()
//MARK: Lifecycle
override
func
viewDidLoad
()
{
if
self
.
path
==
nil
{
let
documentsUrl
=
fileManager
.
URLsForDirectory
(
.
DocumentDirectory
,
inDomains
:
.
UserDomainMask
)[
0
]
as
NSURL
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
{
return
1
}
...
...
@@ -70,7 +86,9 @@ class FileBrowser: UIViewController {
guard
let
path
=
path
else
{
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
fileManager
.
fileExistsAtPath
(
newPath
,
isDirectory
:
&
isDirectory
)
cell
.
textLabel
?
.
text
=
files
[
indexPath
.
row
]
...
...
@@ -80,12 +98,69 @@ class FileBrowser: UIViewController {
else
{
cell
.
imageView
?
.
image
=
UIImage
(
named
:
"File"
)
}
cell
.
backgroundColor
=
(
selectedFiles
.
contains
(
filePath
))
?
UIColor
(
white
:
0.9
,
alpha
:
1.0
):
UIColor
.
whiteColor
()
return
cell
}
required
init
?(
coder
aDecoder
:
NSCoder
)
{
super
.
init
(
coder
:
aDecoder
)
func
tableView
(
tableView
:
UITableView
,
didSelectRowAtIndexPath
indexPath
:
NSIndexPath
)
{
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"
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment