Skip to content

Commit

Permalink
Add URL extension
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Nov 6, 2024
1 parent 833bced commit 0a8b5bd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Sources/Zip/ArchiveFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions Sources/Zip/URL+nativePath.swift
Original file line number Diff line number Diff line change
@@ -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!) }
}
}
8 changes: 4 additions & 4 deletions Sources/Zip/Zip+ProcessedFilePath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extension Zip {
let fileName: String?

var filePath: String {
filePathURL.withUnsafeFileSystemRepresentation { String(cString: $0!) }
filePathURL.nativePath
}
}

Expand All @@ -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
)

Expand All @@ -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
)

Expand Down
10 changes: 4 additions & 6 deletions Sources/Zip/Zip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0a8b5bd

Please sign in to comment.