FileUtil.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // FileUtil.swift
  3. // CommonUtil
  4. //
  5. // Created by lijunjie on 15/11/14.
  6. // Copyright © 2015年 lijunjie. All rights reserved.
  7. //
  8. import Foundation
  9. public class FileUtil {
  10. static let share = FileUtil()
  11. private init () {}
  12. public func createDirectory(path: String) {
  13. if !directoryExist(directoryPath: path) {
  14. do {
  15. try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
  16. } catch {
  17. print("创建目录错误!")
  18. }
  19. }
  20. }
  21. public func fileExist(filePath: String) -> Bool {
  22. return FileManager.default.fileExists(atPath: filePath, isDirectory: nil)
  23. }
  24. /// 获取本地文件的最后修改时间
  25. public func fileModificationDate(filePath: String) -> NSDate? {
  26. if let attribute = try? FileManager.default.attributesOfItem(atPath: filePath) {
  27. return attribute[.modificationDate] as? NSDate
  28. }
  29. return nil
  30. }
  31. public func deleteFile(filePath: String) -> Bool {
  32. do {
  33. try FileManager.default.removeItem(atPath: filePath)
  34. return true
  35. } catch {
  36. return false
  37. }
  38. }
  39. public func directoryExist(directoryPath: String) -> Bool {
  40. var isDir:ObjCBool = true
  41. return FileManager.default.fileExists(atPath: directoryPath, isDirectory: &isDir)
  42. }
  43. public func writeFileData(data:NSData, toPath: String) -> Bool {
  44. return data.write(toFile: toPath, atomically: true)
  45. }
  46. public func readFromFile(path: String) -> NSData? {
  47. return NSData(contentsOfFile: path)
  48. }
  49. public func deleteFileAtPath(filePath: String) {
  50. do {
  51. try FileManager.default.removeItem(atPath: filePath)
  52. } catch {
  53. print("删除目录里面的文件错误!")
  54. }
  55. }
  56. public func deleteDirectoryAtPath(dirPath: String) {
  57. do {
  58. try FileManager.default.removeItem(atPath: dirPath)
  59. } catch {
  60. print("删除目录里面的目录错误!")
  61. }
  62. }
  63. public func copyFileFromPath(fromPath: String, toPath: String, isRemoveOld: Bool) -> Bool {
  64. var res = false
  65. if self.fileExist(filePath: fromPath) {
  66. do {
  67. try FileManager.default.copyItem(atPath: fromPath, toPath: toPath)
  68. res = true
  69. } catch {
  70. print("复制文件错误!")
  71. }
  72. } else {
  73. print("源文件不存在!")
  74. }
  75. if res && isRemoveOld {
  76. do {
  77. try FileManager.default.removeItem(atPath: fromPath)
  78. } catch {
  79. print("删除当前路径")
  80. }
  81. }
  82. return res
  83. }
  84. public func documentDirectory() -> String {
  85. return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
  86. }
  87. public func documentDirectoryPath(file: String) -> String {
  88. return (self.documentDirectory() as NSString).appendingPathComponent(file)
  89. }
  90. public func cacheDirectory() -> String {
  91. return NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
  92. }
  93. public func cacheDir() -> URL {
  94. return FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
  95. }
  96. public func cacheDirectoryPath(file: String) -> String {
  97. return (self.cacheDirectory() as NSString).appendingPathComponent(file)
  98. }
  99. public func cacheDirectoryDelete(file: String) -> Bool {
  100. return self.deleteFile(filePath: self.cacheDirectoryPath(file: file))
  101. }
  102. public func documentDirectoryDelete(file: String) -> Bool {
  103. return self.deleteFile(filePath: self.documentDirectoryPath(file: file))
  104. }
  105. public func getDirFileNames(dirPath: String) -> [String] {
  106. var arrTemp = [NSURL]()
  107. do {
  108. arrTemp = try FileManager.default.contentsOfDirectory(at: NSURL.fileURL(withPath: dirPath), includingPropertiesForKeys: [URLResourceKey.nameKey], options: FileManager.DirectoryEnumerationOptions.skipsHiddenFiles) as [NSURL]
  109. } catch {
  110. return Array()
  111. }
  112. if arrTemp.count == 0 {
  113. return Array()
  114. }
  115. var arr = [String]()
  116. for fileNameUrl in arrTemp {
  117. arr.append(fileNameUrl.relativePath!)
  118. }
  119. return arr
  120. }
  121. public func saveImage(path: NSString) {
  122. let lastDir = path.deletingLastPathComponent
  123. if FileManager.default.fileExists(atPath: lastDir, isDirectory: nil) {
  124. try! FileManager.default.createDirectory(atPath: lastDir, withIntermediateDirectories: true, attributes: nil)
  125. }
  126. }
  127. }
  128. public let SharedFileUtil: FileUtil = FileUtil.share