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
Hide 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 {
...
@@ -37,7 +37,7 @@ extension Zip {
*/
*/
public
func
quickUnzipFile
(
path
:
NSURL
,
progress
:
((
progress
:
Double
)
->
())?)
throws
->
NSURL
{
public
func
quickUnzipFile
(
path
:
NSURL
,
progress
:
((
progress
:
Double
)
->
())?)
throws
->
NSURL
{
guard
let
fileExtension
=
path
.
pathExtension
,
let
fileName
=
path
.
lastPathComponent
else
{
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
directoryName
=
fileName
.
stringByReplacingOccurrencesOfString
(
fileExtension
,
withString
:
""
)
let
destinationUrl
=
documentsUrl
.
URLByAppendingPathComponent
(
directoryName
,
isDirectory
:
true
)
let
destinationUrl
=
documentsUrl
.
URLByAppendingPathComponent
(
directoryName
,
isDirectory
:
true
)
...
...
Zip/Zip.swift
View file @
1081f87f
...
@@ -12,17 +12,15 @@ import minizip
...
@@ -12,17 +12,15 @@ import minizip
/// Zip error type
/// Zip error type
public
enum
ZipError
:
ErrorType
{
public
enum
ZipError
:
ErrorType
{
case
FileNotFound
// File not found
case
FileNotFound
// File not found
case
UnzipError
// Unzip error
case
UnzipFail
// Unzip error
case
ZipError
// Unzip error
case
ZipFail
// Zip error
case
NotAZipFileError
// Unzip error
/// Description variable
/// Description variable
public
var
description
:
String
{
public
var
description
:
String
{
switch
self
{
switch
self
{
case
.
FileNotFound
:
return
NSLocalizedString
(
"File not found."
,
comment
:
""
)
case
.
FileNotFound
:
return
NSLocalizedString
(
"File not found."
,
comment
:
""
)
case
.
UnzipError
:
return
NSLocalizedString
(
"Failed to unzip zip file."
,
comment
:
""
)
case
.
UnzipFail
:
return
NSLocalizedString
(
"Failed to unzip zip file."
,
comment
:
""
)
case
.
ZipError
:
return
NSLocalizedString
(
"Failed to zip file."
,
comment
:
""
)
case
.
ZipFail
:
return
NSLocalizedString
(
"Failed to zip file."
,
comment
:
""
)
case
.
NotAZipFileError
:
return
NSLocalizedString
(
"The file path does not contain a zip file."
,
comment
:
""
)
}
}
}
}
}
}
...
@@ -57,9 +55,9 @@ public class Zip {
...
@@ -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.
- 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
{
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
()
let
fileManager
=
NSFileManager
.
defaultManager
()
if
fileManager
.
fileExistsAtPath
(
path
.
absoluteString
)
==
false
{
if
fileManager
.
fileExistsAtPath
(
path
.
absoluteString
)
==
false
||
path
.
pathExtension
!=
".zip"
{
throw
ZipError
.
FileNotFound
throw
ZipError
.
FileNotFound
}
}
// Unzip set up
// Unzip set up
...
@@ -71,10 +69,10 @@ public class Zip {
...
@@ -71,10 +69,10 @@ public class Zip {
let
zip
=
unzOpen64
(
path
.
absoluteString
)
let
zip
=
unzOpen64
(
path
.
absoluteString
)
let
fileAttributes
=
try
fileManager
.
attributesOfItemAtPath
(
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
var
currentPosition
:
Double
=
0.0
if
unzGoToFirstFile
(
zip
)
!=
UNZ_OK
{
if
unzGoToFirstFile
(
zip
)
!=
UNZ_OK
{
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
}
repeat
{
repeat
{
if
let
cPassword
=
password
?
.
cStringUsingEncoding
(
NSASCIIStringEncoding
)
{
if
let
cPassword
=
password
?
.
cStringUsingEncoding
(
NSASCIIStringEncoding
)
{
...
@@ -84,25 +82,25 @@ public class Zip {
...
@@ -84,25 +82,25 @@ public class Zip {
ret
=
unzOpenCurrentFile
(
zip
);
ret
=
unzOpenCurrentFile
(
zip
);
}
}
if
ret
!=
UNZ_OK
{
if
ret
!=
UNZ_OK
{
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
}
var
fileInfo
=
unz_file_info64
()
var
fileInfo
=
unz_file_info64
()
memset
(
&
fileInfo
,
0
,
sizeof
(
unz_file_info
))
memset
(
&
fileInfo
,
0
,
sizeof
(
unz_file_info
))
ret
=
unzGetCurrentFileInfo64
(
zip
,
&
fileInfo
,
nil
,
0
,
nil
,
0
,
nil
,
0
)
ret
=
unzGetCurrentFileInfo64
(
zip
,
&
fileInfo
,
nil
,
0
,
nil
,
0
,
nil
,
0
)
if
ret
!=
UNZ_OK
{
if
ret
!=
UNZ_OK
{
unzCloseCurrentFile
(
zip
)
unzCloseCurrentFile
(
zip
)
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
}
currentPosition
+=
Double
(
fileInfo
.
compressed_size
)
currentPosition
+=
Double
(
fileInfo
.
compressed_size
)
let
fileNameSize
=
Int
(
fileInfo
.
size_filename
)
+
1
let
fileNameSize
=
Int
(
fileInfo
.
size_filename
)
+
1
let
fileName
=
UnsafeMutablePointer
<
CChar
>.
alloc
(
fileNameSize
)
let
fileName
=
UnsafeMutablePointer
<
CChar
>.
alloc
(
fileNameSize
)
if
fileName
==
nil
{
if
fileName
==
nil
{
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
}
unzGetCurrentFileInfo64
(
zip
,
&
fileInfo
,
fileName
,
UInt
(
fileNameSize
),
nil
,
0
,
nil
,
0
)
unzGetCurrentFileInfo64
(
zip
,
&
fileInfo
,
fileName
,
UInt
(
fileNameSize
),
nil
,
0
,
nil
,
0
)
fileName
[
Int
(
fileInfo
.
size_filename
)]
=
0
fileName
[
Int
(
fileInfo
.
size_filename
)]
=
0
guard
var
pathString
=
String
(
CString
:
fileName
,
encoding
:
NSUTF8StringEncoding
)
else
{
guard
var
pathString
=
String
(
CString
:
fileName
,
encoding
:
NSUTF8StringEncoding
)
else
{
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
}
var
isDirectory
=
false
var
isDirectory
=
false
let
fileInfoSizeFileName
=
Int
(
fileInfo
.
size_filename
-
1
)
let
fileInfoSizeFileName
=
Int
(
fileInfo
.
size_filename
-
1
)
...
@@ -114,7 +112,7 @@ public class Zip {
...
@@ -114,7 +112,7 @@ public class Zip {
pathString
=
pathString
.
stringByReplacingOccurrencesOfString
(
"
\\
"
,
withString
:
"/"
)
pathString
=
pathString
.
stringByReplacingOccurrencesOfString
(
"
\\
"
,
withString
:
"/"
)
}
}
guard
let
fullPath
=
destination
.
URLByAppendingPathComponent
(
pathString
)
.
path
else
{
guard
let
fullPath
=
destination
.
URLByAppendingPathComponent
(
pathString
)
.
path
else
{
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
}
let
creationDate
=
NSDate
()
let
creationDate
=
NSDate
()
let
directoryAttributes
=
[
NSFileCreationDate
:
creationDate
,
NSFileModificationDate
:
creationDate
]
let
directoryAttributes
=
[
NSFileCreationDate
:
creationDate
,
NSFileModificationDate
:
creationDate
]
...
@@ -144,10 +142,10 @@ public class Zip {
...
@@ -144,10 +142,10 @@ public class Zip {
fclose
(
filePointer
)
fclose
(
filePointer
)
crc_ret
=
unzCloseCurrentFile
(
zip
)
crc_ret
=
unzCloseCurrentFile
(
zip
)
if
crc_ret
==
UNZ_CRCERROR
{
if
crc_ret
==
UNZ_CRCERROR
{
throw
ZipError
.
Unzip
Error
throw
ZipError
.
Unzip
Fail
}
}
if
let
progressHandler
=
progress
,
let
fileSize
=
file
Size
{
if
let
progressHandler
=
progress
,
let
totalSize
=
total
Size
{
progressHandler
(
progress
:
(
currentPosition
/
file
Size
))
progressHandler
(
progress
:
(
currentPosition
/
total
Size
))
}
}
ret
=
unzGoToNextFile
(
zip
)
ret
=
unzGoToNextFile
(
zip
)
}
while
(
ret
==
UNZ_OK
&&
ret
!=
UNZ_END_OF_LIST_OF_FILE
)
}
while
(
ret
==
UNZ_OK
&&
ret
!=
UNZ_END_OF_LIST_OF_FILE
)
...
@@ -174,24 +172,25 @@ public class Zip {
...
@@ -174,24 +172,25 @@ public class Zip {
let
fileManager
=
NSFileManager
.
defaultManager
()
let
fileManager
=
NSFileManager
.
defaultManager
()
var
currentPosition
:
Double
=
0.0
var
currentPosition
:
Double
=
0.0
var
totalSize
:
Double
=
0.0
var
totalSize
:
Double
=
0.0
// If progress handler exists, get total fileSize
// Check if paths exist and get totalSize for progress handler
if
progress
!=
nil
{
for
path
in
paths
{
for
path
in
paths
{
if
fileManager
.
fileExistsAtPath
(
path
.
absoluteString
)
==
false
{
do
{
throw
ZipError
.
FileNotFound
let
fileAttributes
=
try
fileManager
.
attributesOfItemAtPath
(
path
.
path
!
)
}
let
fileSize
=
fileAttributes
[
NSFileSize
]
as?
Double
do
{
if
let
fileSize
=
fileSize
{
let
fileAttributes
=
try
fileManager
.
attributesOfItemAtPath
(
path
.
path
!
)
totalSize
+=
fileSize
let
fileSize
=
fileAttributes
[
NSFileSize
]
as?
Double
}
if
let
fileSize
=
fileSize
{
totalSize
+=
fileSize
}
}
catch
{}
}
}
catch
{}
}
}
let
zip
=
zipOpen
(
destination
.
path
!
,
APPEND_STATUS_CREATE
)
let
zip
=
zipOpen
(
destination
.
path
!
,
APPEND_STATUS_CREATE
)
for
path
in
paths
{
for
path
in
paths
{
let
input
=
fopen
(
path
.
path
!
,
"r"
)
let
input
=
fopen
(
path
.
path
!
,
"r"
)
if
input
==
nil
{
if
input
==
nil
{
throw
ZipError
.
Zip
Error
throw
ZipError
.
Zip
Fail
}
}
let
fileName
=
path
.
lastPathComponent
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
)
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 {
...
@@ -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
)
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
{
else
{
throw
ZipError
.
Zip
Error
throw
ZipError
.
Zip
Fail
}
}
var
len
:
Int
=
0
var
len
gth
:
Int
=
0
while
(
feof
(
input
)
==
0
)
{
while
(
feof
(
input
)
==
0
)
{
len
=
fread
(
buffer
,
1
,
chunkSize
,
input
)
len
gth
=
fread
(
buffer
,
1
,
chunkSize
,
input
)
zipWriteInFileInZip
(
zip
,
buffer
,
UInt32
(
len
))
zipWriteInFileInZip
(
zip
,
buffer
,
UInt32
(
len
gth
))
}
}
if
let
progressHandler
=
progress
{
if
let
progressHandler
=
progress
{
progressHandler
(
progress
:
(
currentPosition
/
totalSize
))
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