Skip to content

Commit

Permalink
Apply Saleem changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Nov 7, 2024
1 parent 145d03d commit d271668
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Sources/Zip/ArchiveFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
)

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
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!) }
}
}
25 changes: 12 additions & 13 deletions Sources/Zip/Zip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

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

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

0 comments on commit d271668

Please sign in to comment.