forked from marmelroy/Zip
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
567d515
commit 9c6ae98
Showing
6 changed files
with
377 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Foundation | ||
|
||
extension Zip { | ||
struct ProcessedFilePath { | ||
let filePathURL: URL | ||
let fileName: String? | ||
|
||
var filePath: String { | ||
filePathURL.withUnsafeFileSystemRepresentation { String(cString: $0!) } | ||
} | ||
} | ||
|
||
/// Process zip paths. | ||
/// | ||
/// - Parameter paths: Paths as `URL`. | ||
/// | ||
/// - Returns: Array of ``ProcessedFilePath`` structs. | ||
static func processZipPaths(_ paths: [URL]) -> [ProcessedFilePath] { | ||
var processedFilePaths = [ProcessedFilePath]() | ||
for pathURL in paths { | ||
var isDirectory: ObjCBool = false | ||
_ = FileManager.default.fileExists( | ||
atPath: pathURL.withUnsafeFileSystemRepresentation { String(cString: $0!) }, | ||
isDirectory: &isDirectory | ||
) | ||
|
||
if !isDirectory.boolValue { | ||
let processedPath = ProcessedFilePath(filePathURL: pathURL, fileName: pathURL.lastPathComponent) | ||
processedFilePaths.append(processedPath) | ||
} else { | ||
let directoryContents = Self.expandDirectoryFilePath(pathURL) | ||
processedFilePaths.append(contentsOf: directoryContents) | ||
} | ||
} | ||
return processedFilePaths | ||
} | ||
|
||
/// Expand directory contents and parse them into ``ProcessedFilePath`` structs. | ||
/// | ||
/// - Parameter directory: Path of folder as `URL`. | ||
/// | ||
/// - 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!) }) { | ||
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!) }, | ||
isDirectory: &isDirectory | ||
) | ||
|
||
if !isDirectory.boolValue { | ||
let fileName = (directory.lastPathComponent as NSString).appendingPathComponent(filePathComponent) | ||
let processedPath = ProcessedFilePath(filePathURL: pathURL, fileName: fileName) | ||
processedFilePaths.append(processedPath) | ||
} | ||
} | ||
} | ||
return processedFilePaths | ||
} | ||
} |
Oops, something went wrong.