Skip to content

Commit

Permalink
Fix Linux bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Aug 26, 2024
1 parent 69f0bba commit 089084f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
1 change: 0 additions & 1 deletion Sources/Zip/ArchiveFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Sources/Zip/QuickZip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}
2 changes: 2 additions & 0 deletions Sources/Zip/Zip.docc/Quick.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ do {

### Quick Functions

- ``Zip/quickZipFiles(_:fileName:)``
- ``Zip/quickZipFiles(_:fileName:progress:)``
- ``Zip/quickUnzipFile(_:)``
- ``Zip/quickUnzipFile(_:progress:)``
32 changes: 13 additions & 19 deletions Sources/Zip/Zip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

0 comments on commit 089084f

Please sign in to comment.