FileUtil.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. public func deleteFile(filePath: String) -> Bool {
  25. do {
  26. try FileManager.default.removeItem(atPath: filePath)
  27. return true
  28. } catch {
  29. return false
  30. }
  31. }
  32. public func directoryExist(directoryPath: String) -> Bool {
  33. var isDir:ObjCBool = true
  34. return FileManager.default.fileExists(atPath: directoryPath, isDirectory: &isDir)
  35. }
  36. public func writeFileData(data:NSData, toPath: String) -> Bool {
  37. return data.write(toFile: toPath, atomically: true)
  38. }
  39. public func readFromFile(path: String) -> NSData? {
  40. return NSData(contentsOfFile: path)
  41. }
  42. public func deleteFileAtPath(filePath: String) {
  43. do {
  44. try FileManager.default.removeItem(atPath: filePath)
  45. } catch {
  46. print("删除目录里面的文件错误!")
  47. }
  48. }
  49. public func deleteDirectoryAtPath(dirPath: String) {
  50. do {
  51. try FileManager.default.removeItem(atPath: dirPath)
  52. } catch {
  53. print("删除目录里面的目录错误!")
  54. }
  55. }
  56. public func copyFileFromPath(fromPath: String, toPath: String, isRemoveOld: Bool) -> Bool {
  57. var res = false
  58. if self.fileExist(filePath: fromPath) {
  59. do {
  60. try FileManager.default.copyItem(atPath: fromPath, toPath: toPath)
  61. res = true
  62. } catch {
  63. print("复制文件错误!")
  64. }
  65. } else {
  66. print("源文件不存在!")
  67. }
  68. if res && isRemoveOld {
  69. do {
  70. try FileManager.default.removeItem(atPath: fromPath)
  71. } catch {
  72. print("删除当前路径")
  73. }
  74. }
  75. return res
  76. }
  77. public func documentDirectory() -> String {
  78. return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
  79. }
  80. public func documentDirectoryPath(file: String) -> String {
  81. return (self.documentDirectory() as NSString).appendingPathComponent(file)
  82. }
  83. public func cacheDirectory() -> String {
  84. return NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
  85. }
  86. public func cacheDir() -> URL {
  87. return FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
  88. }
  89. public func cacheDirectoryPath(file: String) -> String {
  90. return (self.cacheDirectory() as NSString).appendingPathComponent(file)
  91. }
  92. public func cacheDirectoryDelete(file: String) -> Bool {
  93. return self.deleteFile(filePath: self.cacheDirectoryPath(file: file))
  94. }
  95. public func documentDirectoryDelete(file: String) -> Bool {
  96. return self.deleteFile(filePath: self.documentDirectoryPath(file: file))
  97. }
  98. public func getDirFileNames(dirPath: String) -> [String] {
  99. var arrTemp = [NSURL]()
  100. do {
  101. arrTemp = try FileManager.default.contentsOfDirectory(at: NSURL.fileURL(withPath: dirPath), includingPropertiesForKeys: [URLResourceKey.nameKey], options: FileManager.DirectoryEnumerationOptions.skipsHiddenFiles) as [NSURL]
  102. } catch {
  103. return Array()
  104. }
  105. if arrTemp.count == 0 {
  106. return Array()
  107. }
  108. var arr = [String]()
  109. for fileNameUrl in arrTemp {
  110. arr.append(fileNameUrl.relativePath!)
  111. }
  112. return arr
  113. }
  114. public func saveImage(path: NSString) {
  115. let lastDir = path.deletingLastPathComponent
  116. if FileManager.default.fileExists(atPath: lastDir, isDirectory: nil) {
  117. try! FileManager.default.createDirectory(atPath: lastDir, withIntermediateDirectories: true, attributes: nil)
  118. }
  119. }
  120. }
  121. public let SharedFileUtil: FileUtil = FileUtil.share