mirror of
https://github.com/tsolomko/SWCompression.git
synced 2026-06-23 14:56:41 +00:00
36 lines
930 B
Swift
36 lines
930 B
Swift
// Copyright (c) 2026 Timofey Solomko
|
|
// Licensed under MIT License
|
|
//
|
|
// See LICENSE for license information
|
|
|
|
import Foundation
|
|
import SWCompression
|
|
import SwiftCLI
|
|
|
|
final class XZCommand: Command {
|
|
|
|
let name = "xz"
|
|
let shortDescription = "Extracts a XZ archive"
|
|
|
|
@Param var input: String
|
|
@Param var output: String?
|
|
|
|
func execute() throws {
|
|
let inputURL = URL(fileURLWithPath: self.input)
|
|
|
|
let outputURL: URL
|
|
if let outputPath = self.output {
|
|
outputURL = URL(fileURLWithPath: outputPath)
|
|
} else if inputURL.pathExtension == "xz" {
|
|
outputURL = inputURL.deletingPathExtension()
|
|
} else {
|
|
swcompExit(.noOutputPath)
|
|
}
|
|
|
|
let fileData = try Data(contentsOf: inputURL, options: .mappedIfSafe)
|
|
let decompressedData = try XZArchive.unarchive(archive: fileData)
|
|
try decompressedData.write(to: outputURL)
|
|
}
|
|
|
|
}
|