From 0a8b5bdf48b3f3924d47879958ec256196ba7a26 Mon Sep 17 00:00:00 2001 From: Francesco Paolo Severino Date: Wed, 6 Nov 2024 23:08:53 +0100 Subject: [PATCH] Add URL extension --- Sources/Zip/ArchiveFile.swift | 2 +- Sources/Zip/URL+nativePath.swift | 11 +++++++++++ Sources/Zip/Zip+ProcessedFilePath.swift | 8 ++++---- Sources/Zip/Zip.swift | 10 ++++------ 4 files changed, 20 insertions(+), 11 deletions(-) create mode 100644 Sources/Zip/URL+nativePath.swift diff --git a/Sources/Zip/ArchiveFile.swift b/Sources/Zip/ArchiveFile.swift index 43143f6c..d72b6bbe 100644 --- a/Sources/Zip/ArchiveFile.swift +++ b/Sources/Zip/ArchiveFile.swift @@ -69,7 +69,7 @@ extension Zip { progressTracker.kind = ProgressKind.file // Begin Zipping - let zip = zipOpen(zipFilePath.withUnsafeFileSystemRepresentation { String(cString: $0!) }, APPEND_STATUS_CREATE) + let zip = zipOpen(zipFilePath.nativePath, APPEND_STATUS_CREATE) for archiveFile in archiveFiles { // Skip empty data diff --git a/Sources/Zip/URL+nativePath.swift b/Sources/Zip/URL+nativePath.swift new file mode 100644 index 00000000..5cfc0211 --- /dev/null +++ b/Sources/Zip/URL+nativePath.swift @@ -0,0 +1,11 @@ +#if canImport(Darwin) || compiler(<6.0) + import Foundation +#else + import FoundationEssentials +#endif + +extension URL { + var nativePath: String { + return withUnsafeFileSystemRepresentation { String(cString: $0!) } + } +} \ No newline at end of file diff --git a/Sources/Zip/Zip+ProcessedFilePath.swift b/Sources/Zip/Zip+ProcessedFilePath.swift index ccfa43bb..2e836d16 100644 --- a/Sources/Zip/Zip+ProcessedFilePath.swift +++ b/Sources/Zip/Zip+ProcessedFilePath.swift @@ -6,7 +6,7 @@ extension Zip { let fileName: String? var filePath: String { - filePathURL.withUnsafeFileSystemRepresentation { String(cString: $0!) } + filePathURL.nativePath } } @@ -20,7 +20,7 @@ extension Zip { for pathURL in paths { var isDirectory: ObjCBool = false _ = FileManager.default.fileExists( - atPath: pathURL.withUnsafeFileSystemRepresentation { String(cString: $0!) }, + atPath: pathURL.nativePath, isDirectory: &isDirectory ) @@ -42,13 +42,13 @@ extension Zip { /// - Returns: Array of ``ProcessedFilePath`` structs. private static func expandDirectoryFilePath(_ directory: URL) -> [ProcessedFilePath] { var processedFilePaths = [ProcessedFilePath]() - if let enumerator = FileManager.default.enumerator(atPath: directory.withUnsafeFileSystemRepresentation { String(cString: $0!) }) { + if let enumerator = FileManager.default.enumerator(atPath: directory.nativePath) { while let filePathComponent = enumerator.nextObject() as? String { let pathURL = directory.appendingPathComponent(filePathComponent) var isDirectory: ObjCBool = false _ = FileManager.default.fileExists( - atPath: pathURL.withUnsafeFileSystemRepresentation { String(cString: $0!) }, + atPath: pathURL.nativePath, isDirectory: &isDirectory ) diff --git a/Sources/Zip/Zip.swift b/Sources/Zip/Zip.swift index 56c6af14..20af5bfd 100644 --- a/Sources/Zip/Zip.swift +++ b/Sources/Zip/Zip.swift @@ -41,7 +41,7 @@ public class Zip { fileOutputHandler: ((_ unzippedFile: URL) -> Void)? = nil ) throws { // Check whether a zip file exists at path. - let path = zipFilePath.withUnsafeFileSystemRepresentation { String(cString: $0!) } + let path = zipFilePath.nativePath if !FileManager.default.fileExists(atPath: path) || !isValidFileExtension(zipFilePath.pathExtension) { throw ZipError.fileNotFound } @@ -129,13 +129,11 @@ public class Zip { pathString = pathString.replacingOccurrences(of: "\\", with: "/") } - let fullPath = destination.appendingPathComponent(pathString).standardizedFileURL.withUnsafeFileSystemRepresentation { - String(cString: $0!) - } + let fullPath = destination.appendingPathComponent(pathString).standardizedFileURL.nativePath // `.standardizedFileURL` removes any `..` to move a level up. // If we then check that the `fullPath` starts with the destination directory we know we are not extracting "outside" the destination. - guard fullPath.starts(with: destination.standardizedFileURL.withUnsafeFileSystemRepresentation { String(cString: $0!) }) else { + guard fullPath.starts(with: destination.standardizedFileURL.nativePath) else { throw ZipError.unzipFail } @@ -274,7 +272,7 @@ public class Zip { progressTracker.kind = ProgressKind.file // Begin Zipping - let zip = zipOpen(zipFilePath.withUnsafeFileSystemRepresentation { String(cString: $0!) }, APPEND_STATUS_CREATE) + let zip = zipOpen(zipFilePath.nativePath, APPEND_STATUS_CREATE) for path in processedPaths { let filePath = path.filePath