用于在 macOS 上复制文件的本机框架
Native framework for copying file on macOS
macOS 中是否有可用于以编程方式复制文件并获得与从 Finder 复制文件相同的进度条的框架?
或者,我可以使用 cp
或文件系统 API 进行复制,并使用 NSProgressIndicator
实现进度条。
我遇到了同样的问题,但我在 swift 中需要它。
但是伟大的 Project Parag 它真的帮助了我!
这就是我的复制功能。
func createCopy(of:URL, to:URL,skip:Bool) throws {
if !skip {
if FileManager.default.fileExists(atPath: to.path) {
print("\n\n did not copy \n\(of.lastPathComponent.utf8CString) \nto \n\(to.path)\n\n")
return
}
}
//open window
let copyStoryboard = NSStoryboard.init(name: "copyProgessWindow", bundle: nil)
self.progressWindowConrtoller = copyStoryboard.instantiateController(withIdentifier: "CopyProgressWindow") as! NSWindowController
let application = NSApplication.shared
//set Up view
let viewController = self.progressWindowConrtoller!.contentViewController as! CopyViewController
viewController.path_copyFrom.url = of
viewController.path_copyTo.url = to
DispatchQueue.global(qos: .userInteractive).async{
do{
let bufferSize:Int = 64*1024;
var buffer = [Int32](repeating: 0, count: Int(bufferSize))
let open_source = open(of.path, O_RDONLY);
let open_target = open(to.path, O_RDWR | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR);
let attrSource = try FileManager.default.attributesOfItem(atPath: of.path)
let sourceFileSize = attrSource[FileAttributeKey.size] as! UInt64
var bytes_read = 0;
bytes_read = read(open_source, &buffer, bufferSize);
while(bytes_read > 0){
write(open_target, &buffer, bufferSize);
if FileManager.default.fileExists(atPath: to.path) {
let attrTarget = try FileManager.default.attributesOfItem(atPath: to.path)
let targetFileSize = attrTarget[FileAttributeKey.size] as! UInt64
DispatchQueue.main.async {
viewController.progressbar_progBar.doubleValue = self.progressProcent
}
self.progressProcent = Double(targetFileSize)/Double(sourceFileSize)
print(self.progressProcent)
}
bytes_read = read(open_source, &buffer, bufferSize);
}
// copy is done, or an error occurred
close(open_source);
close(open_target);
sleep(2)
DispatchQueue.main.async {
application.abortModal()
self.progressWindowConrtoller!.close()
}
}
catch{
print(error)
}
}
application.runModal(for: self.progressWindowConrtoller!.window!)
}
我有一个名为 'copyProgessWindow.storyboard' 的故事板文件,重要的是故事板 ID 设置为 'CopyProgressWindow'。它看起来像这样:
storyboardCopyProgress
这就是我的 ViewController 您看到的视图:
class 复制ViewController: NSViewController {
@IBOutlet weak var path_copyFrom: NSPathControl!
@IBOutlet weak var path_copyTo: NSPathControl!
@IBOutlet weak var label_remaindingTime: NSTextField!
@IBOutlet weak var progressbar_progBar: NSProgressIndicator!
override func viewDidLoad() {
super.viewDidLoad()
}
}
macOS 中是否有可用于以编程方式复制文件并获得与从 Finder 复制文件相同的进度条的框架?
或者,我可以使用 cp
或文件系统 API 进行复制,并使用 NSProgressIndicator
实现进度条。
我遇到了同样的问题,但我在 swift 中需要它。 但是伟大的 Project Parag 它真的帮助了我!
这就是我的复制功能。
func createCopy(of:URL, to:URL,skip:Bool) throws {
if !skip {
if FileManager.default.fileExists(atPath: to.path) {
print("\n\n did not copy \n\(of.lastPathComponent.utf8CString) \nto \n\(to.path)\n\n")
return
}
}
//open window
let copyStoryboard = NSStoryboard.init(name: "copyProgessWindow", bundle: nil)
self.progressWindowConrtoller = copyStoryboard.instantiateController(withIdentifier: "CopyProgressWindow") as! NSWindowController
let application = NSApplication.shared
//set Up view
let viewController = self.progressWindowConrtoller!.contentViewController as! CopyViewController
viewController.path_copyFrom.url = of
viewController.path_copyTo.url = to
DispatchQueue.global(qos: .userInteractive).async{
do{
let bufferSize:Int = 64*1024;
var buffer = [Int32](repeating: 0, count: Int(bufferSize))
let open_source = open(of.path, O_RDONLY);
let open_target = open(to.path, O_RDWR | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR);
let attrSource = try FileManager.default.attributesOfItem(atPath: of.path)
let sourceFileSize = attrSource[FileAttributeKey.size] as! UInt64
var bytes_read = 0;
bytes_read = read(open_source, &buffer, bufferSize);
while(bytes_read > 0){
write(open_target, &buffer, bufferSize);
if FileManager.default.fileExists(atPath: to.path) {
let attrTarget = try FileManager.default.attributesOfItem(atPath: to.path)
let targetFileSize = attrTarget[FileAttributeKey.size] as! UInt64
DispatchQueue.main.async {
viewController.progressbar_progBar.doubleValue = self.progressProcent
}
self.progressProcent = Double(targetFileSize)/Double(sourceFileSize)
print(self.progressProcent)
}
bytes_read = read(open_source, &buffer, bufferSize);
}
// copy is done, or an error occurred
close(open_source);
close(open_target);
sleep(2)
DispatchQueue.main.async {
application.abortModal()
self.progressWindowConrtoller!.close()
}
}
catch{
print(error)
}
}
application.runModal(for: self.progressWindowConrtoller!.window!)
}
我有一个名为 'copyProgessWindow.storyboard' 的故事板文件,重要的是故事板 ID 设置为 'CopyProgressWindow'。它看起来像这样: storyboardCopyProgress
这就是我的 ViewController 您看到的视图:
class 复制ViewController: NSViewController {
@IBOutlet weak var path_copyFrom: NSPathControl!
@IBOutlet weak var path_copyTo: NSPathControl!
@IBOutlet weak var label_remaindingTime: NSTextField!
@IBOutlet weak var progressbar_progBar: NSProgressIndicator!
override func viewDidLoad() {
super.viewDidLoad()
}
}