Fix problems in XZ command when outputPath wasn't specified

This commit is contained in:
Timofey Solomko
2017-11-04 23:28:58 +03:00
parent a74efddd64
commit 4776ab95a5
+18 -4
View File
@@ -16,11 +16,25 @@ class XZCommand: Command {
let outputPath = OptionalParameter()
func execute() throws {
let fileData = try Data(contentsOf: URL(fileURLWithPath: self.archive.value),
options: .mappedIfSafe)
let outputPath = self.outputPath.value ?? FileManager.default.currentDirectoryPath
let inputURL = URL(fileURLWithPath: self.archive.value)
let outputURL: URL
if let outputPath = self.outputPath.value {
outputURL = URL(fileURLWithPath: outputPath)
} else if inputURL.pathExtension == "xz" {
outputURL = inputURL.deletingPathExtension()
} else {
print("""
ERROR: Unable to get output path. \
No output parameter was specified. \
Extension was: \(inputURL.pathExtension)
""")
exit(1)
}
let fileData = try Data(contentsOf: inputURL, options: .mappedIfSafe)
let decompressedData = try XZArchive.unarchive(archive: fileData)
try decompressedData.write(to: URL(fileURLWithPath: outputPath))
try decompressedData.write(to: outputURL)
}
}