diff --git a/Zip/Zip.swift b/Zip/Zip.swift index c4696e0ac1128b671cbda920ad21134d6dd764f4..178d22bf66e9ab7950fe40a89d162fa33c326cd2 100644 --- a/Zip/Zip.swift +++ b/Zip/Zip.swift @@ -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)