TSFileManagerTool.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // TSFileManagerTool.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2024/12/26.
  6. //
  7. public class TSFileManagerTool {
  8. /// 获取沙盒 Documents 目录路径
  9. public static var documentsDirectory: URL {
  10. return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
  11. }
  12. /// 获取沙盒 Cache 目录路径
  13. public static var cacheDirectory: URL {
  14. return FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
  15. }
  16. /// 获取沙盒 Temporary 目录路径
  17. public static var temporaryDirectory: URL {
  18. return FileManager.default.temporaryDirectory
  19. }
  20. public static func copyFileWithOverwrite(from sourceURL: URL, to targetURL: URL) {
  21. let fileManager = FileManager.default
  22. do {
  23. removeItem(from: targetURL)
  24. checkFolderAndCreate(from: targetURL)
  25. try fileManager.copyItem(at: sourceURL, to: targetURL)
  26. debugPrint("文件复制成功!")
  27. } catch {
  28. debugPrint("文件复制失败: \(error.localizedDescription)")
  29. }
  30. }
  31. public static func removeItem(from sourceURL: URL) {
  32. let fileManager = FileManager.default
  33. do {
  34. // 如果目标路径存在同名文件,先删除旧文件
  35. if fileManager.fileExists(atPath: sourceURL.path) {
  36. try fileManager.removeItem(at: sourceURL)
  37. }
  38. debugPrint("文件删除成功!")
  39. } catch {
  40. debugPrint("文件删除失败: \(error.localizedDescription)")
  41. }
  42. }
  43. /// 移动文件的方法(自动创建目标文件夹)
  44. /// - Parameters:
  45. /// - sourceURL: 文件的源 URL
  46. /// - destinationURL: 目标 URL
  47. /// - Throws: 如果移动失败,会抛出错误
  48. public static func moveFile(from sourceURL: URL, to destinationURL: URL) {
  49. let fileManager = FileManager.default
  50. // 检查源文件是否存在
  51. guard fileManager.fileExists(atPath: sourceURL.path) else {
  52. let error = NSError(domain: "FileMoveError", code: 404, userInfo: [NSLocalizedDescriptionKey: "源文件不存在"])
  53. debugPrint(error)
  54. return
  55. }
  56. // 获取目标文件夹的路径
  57. let destinationDirectory = destinationURL.deletingLastPathComponent()
  58. do {
  59. // 如果目标文件夹不存在,创建文件夹
  60. if !fileManager.fileExists(atPath: destinationDirectory.path) {
  61. try fileManager.createDirectory(at: destinationDirectory, withIntermediateDirectories: true, attributes: nil)
  62. }
  63. // 检查目标路径是否已经存在文件
  64. if fileManager.fileExists(atPath: destinationURL.path) {
  65. // 如果需要覆盖,可以选择先删除目标文件
  66. try fileManager.removeItem(at: destinationURL)
  67. }
  68. // 尝试移动文件
  69. try fileManager.moveItem(at: sourceURL, to: destinationURL)
  70. } catch {
  71. debugPrint("尝试移动文件失败: \(error.localizedDescription)")
  72. }
  73. }
  74. public static func getFileName(from url: URL, includeExtension: Bool = true) -> String {
  75. if includeExtension {
  76. return url.lastPathComponent
  77. } else {
  78. return url.deletingPathExtension().lastPathComponent
  79. }
  80. }
  81. public static func checkFolderAndCreate(from destinationURL: URL){
  82. let fileManager = FileManager.default
  83. let destinationDirectory = destinationURL.deletingLastPathComponent()
  84. // 如果目标文件夹不存在,创建文件夹
  85. if !fileManager.fileExists(atPath: destinationDirectory.path) {
  86. do {
  87. try fileManager.createDirectory(at: destinationDirectory, withIntermediateDirectories: true, attributes: nil)
  88. } catch {
  89. debugPrint("尝试创建文件夹失败: \(error.localizedDescription)")
  90. }
  91. }
  92. }
  93. // MARK: - 文件操作方法
  94. /// 检查文件或文件夹是否存在
  95. public static func fileExists(at url: URL) -> Bool {
  96. return FileManager.default.fileExists(atPath: url.path)
  97. }
  98. /// 创建文件夹
  99. public static func createDirectory(at url: URL) throws {
  100. if !fileExists(at: url) {
  101. try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
  102. }
  103. }
  104. //获取缓存目录下文件夹路径
  105. public static func getCacheSubPath(at url: URL) ->String? {
  106. let array = url.path.components(separatedBy:"/Caches/")
  107. let cashFilePath = array.last
  108. return cashFilePath
  109. }
  110. }
  111. public extension String {
  112. public var isCachesPath:Bool{
  113. return self.contains("/Caches/")
  114. }
  115. public var fillCachePath:String{
  116. return TSFileManagerTool.cacheDirectory.appendingPathComponent(self).path
  117. }
  118. public var fillCacheURL:URL{
  119. return TSFileManagerTool.cacheDirectory.appendingPathComponent(self)
  120. }
  121. public var cachesLastURLString:String{
  122. let parts = self.components(separatedBy: "/Caches/")
  123. if let last = parts.last {
  124. return last
  125. }
  126. return self
  127. }
  128. }
  129. public extension String {
  130. public var isDocumentPath:Bool{
  131. return self.contains("/Documents/")
  132. }
  133. public var fillDocumentPath:String{
  134. return TSFileManagerTool.documentsDirectory.appendingPathComponent(self).path
  135. }
  136. public var fillDocumentURL:URL{
  137. return TSFileManagerTool.documentsDirectory.appendingPathComponent(self)
  138. }
  139. public var documentLastURLString:String{
  140. let parts = self.components(separatedBy: "/Documents/")
  141. if let last = parts.last {
  142. return last
  143. }
  144. return self
  145. }
  146. }