Commit 63cdd914 authored by Roy Marmelstein's avatar Roy Marmelstein

Improved handling of directories inside a zip file

parent b5a63a0c
......@@ -215,52 +215,56 @@ public class Zip {
guard let filePath = path.path else {
throw ZipError.ZipFail
}
let input = fopen(filePath, "r")
if input == nil {
throw ZipError.ZipFail
}
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)
do {
let fileAttributes = try fileManager.attributesOfItemAtPath(filePath)
if let fileDate = fileAttributes[NSFileModificationDate] as? NSDate {
let components = NSCalendar.currentCalendar().components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: fileDate)
zipInfo.tmz_date.tm_sec = UInt32(components.second)
zipInfo.tmz_date.tm_min = UInt32(components.minute)
zipInfo.tmz_date.tm_hour = UInt32(components.hour)
zipInfo.tmz_date.tm_mday = UInt32(components.day)
zipInfo.tmz_date.tm_mon = UInt32(components.month) - 1
zipInfo.tmz_date.tm_year = UInt32(components.year)
var isDirectory: ObjCBool = false
fileManager.fileExistsAtPath(filePath, isDirectory: &isDirectory)
if !isDirectory {
let input = fopen(filePath, "r")
if input == nil {
throw ZipError.ZipFail
}
if let fileSize = fileAttributes[NSFileSize] as? Double {
currentPosition += fileSize
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)
do {
let fileAttributes = try fileManager.attributesOfItemAtPath(filePath)
if let fileDate = fileAttributes[NSFileModificationDate] as? NSDate {
let components = NSCalendar.currentCalendar().components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: fileDate)
zipInfo.tmz_date.tm_sec = UInt32(components.second)
zipInfo.tmz_date.tm_min = UInt32(components.minute)
zipInfo.tmz_date.tm_hour = UInt32(components.hour)
zipInfo.tmz_date.tm_mday = UInt32(components.day)
zipInfo.tmz_date.tm_mon = UInt32(components.month) - 1
zipInfo.tmz_date.tm_year = UInt32(components.year)
}
if let fileSize = fileAttributes[NSFileSize] as? Double {
currentPosition += fileSize
}
}
catch {}
let buffer = malloc(chunkSize)
if let password = password, let fileName = fileName {
zipOpenNewFileInZip3(zip, fileName, &zipInfo, nil, 0, nil, 0, nil,Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, password, 0)
}
else if let fileName = fileName {
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.ZipFail
}
var length: Int = 0
while (feof(input) == 0) {
length = fread(buffer, 1, chunkSize, input)
zipWriteInFileInZip(zip, buffer, UInt32(length))
}
// Update progress handler
if let progressHandler = progress{
progressHandler(progress: (currentPosition/totalSize))
}
zipCloseFileInZip(zip)
free(buffer)
fclose(input)
}
catch {}
let buffer = malloc(chunkSize)
if let password = password, let fileName = fileName {
zipOpenNewFileInZip3(zip, fileName, &zipInfo, nil, 0, nil, 0, nil,Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, password, 0)
}
else if let fileName = fileName {
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.ZipFail
}
var length: Int = 0
while (feof(input) == 0) {
length = fread(buffer, 1, chunkSize, input)
zipWriteInFileInZip(zip, buffer, UInt32(length))
}
// Update progress handler
if let progressHandler = progress{
progressHandler(progress: (currentPosition/totalSize))
}
zipCloseFileInZip(zip)
free(buffer)
fclose(input)
}
zipClose(zip, nil)
......
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