From 089084fbba24aec4b0ebc6e06f3a8a3db335834b Mon Sep 17 00:00:00 2001 From: Francesco Paolo Severino Date: Mon, 26 Aug 2024 13:33:56 +0200 Subject: [PATCH] Fix Linux bug --- Sources/Zip/ArchiveFile.swift | 1 - Sources/Zip/QuickZip.swift | 8 ++++---- Sources/Zip/Zip.docc/Quick.md | 2 ++ Sources/Zip/Zip.swift | 32 +++++++++++++------------------- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/Sources/Zip/ArchiveFile.swift b/Sources/Zip/ArchiveFile.swift index bf6f6e96..7edeef40 100644 --- a/Sources/Zip/ArchiveFile.swift +++ b/Sources/Zip/ArchiveFile.swift @@ -102,7 +102,6 @@ extension Zip { zipInfo.tmz_date.tm_mday = UInt32(calendar.component(.day, from: modifiedTime)) zipInfo.tmz_date.tm_mon = UInt32(calendar.component(.month, from: modifiedTime)) zipInfo.tmz_date.tm_year = UInt32(calendar.component(.year, from: modifiedTime)) - zipInfo.dosDate = modifiedTime.dosDate } // Write the data as a file to zip diff --git a/Sources/Zip/QuickZip.swift b/Sources/Zip/QuickZip.swift index e9dbb316..eb0aad80 100644 --- a/Sources/Zip/QuickZip.swift +++ b/Sources/Zip/QuickZip.swift @@ -48,13 +48,13 @@ extension Zip { - Returns: `URL` of the destination folder. */ - public class func quickUnzipFile(_ path: URL, progress: ((_ progress: Double) -> ())? = nil) throws -> URL { + public class func quickUnzipFile(_ path: URL, progress: ((_ progress: Double) -> ())?) throws -> URL { let fileExtension = path.pathExtension let fileName = path.lastPathComponent let directoryName = fileName.replacingOccurrences(of: ".\(fileExtension)", with: "") let documentsUrl = FileManager.default.urls(for: self.searchPathDirectory, in: .userDomainMask)[0] let destinationUrl = documentsUrl.appendingPathComponent(directoryName, isDirectory: true) - try self.unzipFile(path, destination: destinationUrl, overwrite: true, password: nil, progress: progress) + try self.unzipFile(path, destination: destinationUrl, progress: progress) return destinationUrl } @@ -89,10 +89,10 @@ extension Zip { - Returns: `URL` of the destination folder. */ - public class func quickZipFiles(_ paths: [URL], fileName: String, progress: ((_ progress: Double) -> ())? = nil) throws -> URL { + public class func quickZipFiles(_ paths: [URL], fileName: String, progress: ((_ progress: Double) -> ())?) throws -> URL { let documentsUrl = FileManager.default.urls(for: self.searchPathDirectory, in: .userDomainMask)[0] as URL let destinationUrl = documentsUrl.appendingPathComponent("\(fileName).zip") - try self.zipFiles(paths: paths, zipFilePath: destinationUrl, password: nil, progress: progress) + try self.zipFiles(paths: paths, zipFilePath: destinationUrl, progress: progress) return destinationUrl } } diff --git a/Sources/Zip/Zip.docc/Quick.md b/Sources/Zip/Zip.docc/Quick.md index d8b6f2c2..3cd2435d 100644 --- a/Sources/Zip/Zip.docc/Quick.md +++ b/Sources/Zip/Zip.docc/Quick.md @@ -25,5 +25,7 @@ do { ### Quick Functions +- ``Zip/quickZipFiles(_:fileName:)`` - ``Zip/quickZipFiles(_:fileName:progress:)`` +- ``Zip/quickUnzipFile(_:)`` - ``Zip/quickUnzipFile(_:progress:)`` diff --git a/Sources/Zip/Zip.swift b/Sources/Zip/Zip.swift index a743d959..836083bf 100644 --- a/Sources/Zip/Zip.swift +++ b/Sources/Zip/Zip.swift @@ -119,10 +119,19 @@ public class Zip { } let creationDate = Date() - let directoryAttributes: [FileAttributeKey: Any]? = [ - .creationDate: creationDate, - .modificationDate: creationDate - ] + let directoryAttributes: [FileAttributeKey: Any]? + #if os(Linux) + // On Linux, setting attributes is not yet really implemented. + // In Swift 4.2, the only settable attribute is `.posixPermissions`. + // See https://github.com/apple/swift-corelibs-foundation/blob/swift-4.2-branch/Foundation/FileManager.swift#L182-L196 + // TODO: Check settable attributes in Swift 5.8+ + directoryAttributes = nil + #else + directoryAttributes = [ + .creationDate: creationDate, + .modificationDate: creationDate + ] + #endif do { if isDirectory { @@ -274,7 +283,6 @@ public class Zip { 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!) - zipInfo.dosDate = fileDate.dosDate } if let fileSize = fileAttributes[FileAttributeKey.size] as? Double { currentPosition += fileSize @@ -349,17 +357,3 @@ public class Zip { return validFileExtensions.contains(fileExtension) } } - -extension Date { - var dosDate: UInt { - let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: self) - let year = UInt(components.year! - 1980) << 25 - let month = UInt(components.month!) << 21 - let day = UInt(components.day!) << 16 - let hour = UInt(components.hour!) << 11 - let minute = UInt(components.minute!) << 5 - let second = UInt(components.second!) >> 1 - - return year | month | day | hour | minute | second - } -} \ No newline at end of file