From d27166879bd1adf6d5a657a7ecee8afb3d0af783 Mon Sep 17 00:00:00 2001 From: Francesco Paolo Severino Date: Thu, 7 Nov 2024 11:55:33 +0100 Subject: [PATCH] Apply Saleem changes --- Sources/Zip/ArchiveFile.swift | 2 +- ...ft => FileManager+ProcessedFilePath.swift} | 16 ++++++------ Sources/Zip/URL+nativePath.swift | 11 ++++++++ Sources/Zip/Zip.swift | 25 +++++++++---------- 4 files changed, 32 insertions(+), 22 deletions(-) rename Sources/Zip/{Zip+ProcessedFilePath.swift => FileManager+ProcessedFilePath.swift} (80%) create mode 100644 Sources/Zip/URL+nativePath.swift diff --git a/Sources/Zip/ArchiveFile.swift b/Sources/Zip/ArchiveFile.swift index aaffcaef..2b032b5c 100644 --- a/Sources/Zip/ArchiveFile.swift +++ b/Sources/Zip/ArchiveFile.swift @@ -67,7 +67,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/Zip+ProcessedFilePath.swift b/Sources/Zip/FileManager+ProcessedFilePath.swift similarity index 80% rename from Sources/Zip/Zip+ProcessedFilePath.swift rename to Sources/Zip/FileManager+ProcessedFilePath.swift index ccfa43bb..0b399ec6 100644 --- a/Sources/Zip/Zip+ProcessedFilePath.swift +++ b/Sources/Zip/FileManager+ProcessedFilePath.swift @@ -1,26 +1,26 @@ import Foundation -extension Zip { +extension FileManager { struct ProcessedFilePath { let filePathURL: URL let fileName: String? var filePath: String { - filePathURL.withUnsafeFileSystemRepresentation { String(cString: $0!) } + filePathURL.nativePath } } /// Process zip paths. /// - /// - Parameter paths: Paths as `URL`. + /// - Parameter roots: Paths as `URL`. /// /// - Returns: Array of ``ProcessedFilePath`` structs. - static func processZipPaths(_ paths: [URL]) -> [ProcessedFilePath] { + static func fileSubPaths(from roots: [URL]) -> [ProcessedFilePath] { var processedFilePaths = [ProcessedFilePath]() - for pathURL in paths { + for pathURL in roots { 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/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.swift b/Sources/Zip/Zip.swift index 61a537c9..3670c5ab 100644 --- a/Sources/Zip/Zip.swift +++ b/Sources/Zip/Zip.swift @@ -44,7 +44,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 } @@ -132,13 +132,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 } @@ -158,15 +156,16 @@ public class Zip { || fileName[Int(fileInfo.size_filename - 1)] == "\\".cString(using: String.Encoding.utf8)?.first do { + try FileManager.default.createDirectory( + atPath: (fullPath as NSString).deletingLastPathComponent, + withIntermediateDirectories: true, + attributes: directoryAttributes + ) + if isDirectory { try FileManager.default.createDirectory( atPath: fullPath, - withIntermediateDirectories: true, - attributes: directoryAttributes) - } else { - try FileManager.default.createDirectory( - atPath: (fullPath as NSString).deletingLastPathComponent, - withIntermediateDirectories: true, + withIntermediateDirectories: false, attributes: directoryAttributes ) } @@ -251,7 +250,7 @@ public class Zip { compression: ZipCompression = .DefaultCompression, progress: ((_ progress: Double) -> Void)? = nil ) throws { - let processedPaths = Self.processZipPaths(paths) + let processedPaths = FileManager.fileSubPaths(from: paths) let chunkSize = 16384 @@ -274,7 +273,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