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
1081f87f
Commit
1081f87f
authored
Jan 16, 2016
by
Roy Marmelstein
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean up
parent
c044cb05
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
34 deletions
+33
-34
Zip/QuickZip.swift
Zip/QuickZip.swift
+1
-1
Zip/Zip.swift
Zip/Zip.swift
+32
-33
No files found.
Zip/QuickZip.swift
View file @
1081f87f
...
...
@@ -37,7 +37,7 @@ extension Zip {
*/
public
func
quickUnzipFile
(
path
:
NSURL
,
progress
:
((
progress
:
Double
)
->
())?)
throws
->
NSURL
{
guard
let
fileExtension
=
path
.
pathExtension
,
let
fileName
=
path
.
lastPathComponent
else
{
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
let
directoryName
=
fileName
.
stringByReplacingOccurrencesOfString
(
fileExtension
,
withString
:
""
)
let
destinationUrl
=
documentsUrl
.
URLByAppendingPathComponent
(
directoryName
,
isDirectory
:
true
)
...
...
Zip/Zip.swift
View file @
1081f87f
...
...
@@ -12,17 +12,15 @@ import minizip
/// Zip error type
public
enum
ZipError
:
ErrorType
{
case
FileNotFound
// File not found
case
UnzipError
// Unzip error
case
ZipError
// Unzip error
case
NotAZipFileError
// Unzip error
case
UnzipFail
// Unzip error
case
ZipFail
// Zip error
/// Description variable
public
var
description
:
String
{
switch
self
{
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
:
""
)
case
.
UnzipFail
:
return
NSLocalizedString
(
"Failed to unzip zip file."
,
comment
:
""
)
case
.
ZipFail
:
return
NSLocalizedString
(
"Failed to zip file."
,
comment
:
""
)
}
}
}
...
...
@@ -57,9 +55,9 @@ public class Zip {
- throws: Error if unzipping fails or if fail is not found. Can be printed with a description variable.
*/
public
func
unzipFile
(
path
:
NSURL
,
destination
:
NSURL
,
overwrite
:
Bool
,
password
:
String
?,
progress
:
((
progress
:
Double
)
->
())?)
throws
{
// Check file exists at path.
// Check
whether a zip
file exists at path.
let
fileManager
=
NSFileManager
.
defaultManager
()
if
fileManager
.
fileExistsAtPath
(
path
.
absoluteString
)
==
false
{
if
fileManager
.
fileExistsAtPath
(
path
.
absoluteString
)
==
false
||
path
.
pathExtension
!=
".zip"
{
throw
ZipError
.
FileNotFound
}
// Unzip set up
...
...
@@ -71,10 +69,10 @@ public class Zip {
let
zip
=
unzOpen64
(
path
.
absoluteString
)
let
fileAttributes
=
try
fileManager
.
attributesOfItemAtPath
(
path
.
absoluteString
)
let
file
Size
=
fileAttributes
[
NSFileSize
]
as?
Double
let
total
Size
=
fileAttributes
[
NSFileSize
]
as?
Double
var
currentPosition
:
Double
=
0.0
if
unzGoToFirstFile
(
zip
)
!=
UNZ_OK
{
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
repeat
{
if
let
cPassword
=
password
?
.
cStringUsingEncoding
(
NSASCIIStringEncoding
)
{
...
...
@@ -84,25 +82,25 @@ public class Zip {
ret
=
unzOpenCurrentFile
(
zip
);
}
if
ret
!=
UNZ_OK
{
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
var
fileInfo
=
unz_file_info64
()
memset
(
&
fileInfo
,
0
,
sizeof
(
unz_file_info
))
ret
=
unzGetCurrentFileInfo64
(
zip
,
&
fileInfo
,
nil
,
0
,
nil
,
0
,
nil
,
0
)
if
ret
!=
UNZ_OK
{
unzCloseCurrentFile
(
zip
)
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
currentPosition
+=
Double
(
fileInfo
.
compressed_size
)
let
fileNameSize
=
Int
(
fileInfo
.
size_filename
)
+
1
let
fileName
=
UnsafeMutablePointer
<
CChar
>.
alloc
(
fileNameSize
)
if
fileName
==
nil
{
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
unzGetCurrentFileInfo64
(
zip
,
&
fileInfo
,
fileName
,
UInt
(
fileNameSize
),
nil
,
0
,
nil
,
0
)
fileName
[
Int
(
fileInfo
.
size_filename
)]
=
0
guard
var
pathString
=
String
(
CString
:
fileName
,
encoding
:
NSUTF8StringEncoding
)
else
{
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
var
isDirectory
=
false
let
fileInfoSizeFileName
=
Int
(
fileInfo
.
size_filename
-
1
)
...
...
@@ -114,7 +112,7 @@ public class Zip {
pathString
=
pathString
.
stringByReplacingOccurrencesOfString
(
"
\\
"
,
withString
:
"/"
)
}
guard
let
fullPath
=
destination
.
URLByAppendingPathComponent
(
pathString
)
.
path
else
{
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
let
creationDate
=
NSDate
()
let
directoryAttributes
=
[
NSFileCreationDate
:
creationDate
,
NSFileModificationDate
:
creationDate
]
...
...
@@ -144,10 +142,10 @@ public class Zip {
fclose
(
filePointer
)
crc_ret
=
unzCloseCurrentFile
(
zip
)
if
crc_ret
==
UNZ_CRCERROR
{
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
if
let
progressHandler
=
progress
,
let
fileSize
=
file
Size
{
progressHandler
(
progress
:
(
currentPosition
/
file
Size
))
if
let
progressHandler
=
progress
,
let
totalSize
=
total
Size
{
progressHandler
(
progress
:
(
currentPosition
/
total
Size
))
}
ret
=
unzGoToNextFile
(
zip
)
}
while
(
ret
==
UNZ_OK
&&
ret
!=
UNZ_END_OF_LIST_OF_FILE
)
...
...
@@ -174,9 +172,11 @@ public class Zip {
let
fileManager
=
NSFileManager
.
defaultManager
()
var
currentPosition
:
Double
=
0.0
var
totalSize
:
Double
=
0.0
// If progress handler exists, get total fileSize
if
progress
!=
nil
{
// Check if paths exist and get totalSize for progress handler
for
path
in
paths
{
if
fileManager
.
fileExistsAtPath
(
path
.
absoluteString
)
==
false
{
throw
ZipError
.
FileNotFound
}
do
{
let
fileAttributes
=
try
fileManager
.
attributesOfItemAtPath
(
path
.
path
!
)
let
fileSize
=
fileAttributes
[
NSFileSize
]
as?
Double
...
...
@@ -186,12 +186,11 @@ public class Zip {
}
catch
{}
}
}
let
zip
=
zipOpen
(
destination
.
path
!
,
APPEND_STATUS_CREATE
)
for
path
in
paths
{
let
input
=
fopen
(
path
.
path
!
,
"r"
)
if
input
==
nil
{
throw
ZipError
.
Zip
Error
throw
ZipError
.
Zip
Fail
}
let
fileName
=
path
.
lastPathComponent
var
zipInfo
:
zip_fileinfo
=
zip_fileinfo
(
tmz_date
:
tm_zip
(
tm_sec
:
0
,
tm_min
:
0
,
tm_hour
:
0
,
tm_mday
:
0
,
tm_mon
:
0
,
tm_year
:
0
),
dosDate
:
0
,
internal_fa
:
0
,
external_fa
:
0
)
...
...
@@ -219,12 +218,12 @@ public class Zip {
zipOpenNewFileInZip3
(
zip
,
fileName
,
&
zipInfo
,
nil
,
0
,
nil
,
0
,
nil
,
Z_DEFLATED
,
Z_DEFAULT_COMPRESSION
,
0
,
-
MAX_WBITS
,
DEF_MEM_LEVEL
,
Z_DEFAULT_STRATEGY
,
nil
,
0
)
}
else
{
throw
ZipError
.
Zip
Error
throw
ZipError
.
Zip
Fail
}
var
len
:
Int
=
0
var
len
gth
:
Int
=
0
while
(
feof
(
input
)
==
0
)
{
len
=
fread
(
buffer
,
1
,
chunkSize
,
input
)
zipWriteInFileInZip
(
zip
,
buffer
,
UInt32
(
len
))
len
gth
=
fread
(
buffer
,
1
,
chunkSize
,
input
)
zipWriteInFileInZip
(
zip
,
buffer
,
UInt32
(
len
gth
))
}
if
let
progressHandler
=
progress
{
progressHandler
(
progress
:
(
currentPosition
/
totalSize
))
...
...
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