RQFileManager.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // RQFileManager.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/21.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "RQFileManager.h"
  9. @implementation RQFileManager
  10. /**
  11. * 文件管理器
  12. */
  13. + (NSFileManager *)fileManager
  14. {
  15. return [NSFileManager defaultManager];
  16. }
  17. /**
  18. * 该路径是否存在
  19. */
  20. + (BOOL)isPathExist:(NSString *)path
  21. {
  22. return [[self fileManager] fileExistsAtPath:path];
  23. }
  24. /**
  25. * 该文件是否存在
  26. */
  27. + (BOOL)isFileExist:(NSString *)path
  28. {
  29. BOOL isDirectory;
  30. return [[self fileManager] fileExistsAtPath:path isDirectory:&isDirectory] && !isDirectory;
  31. }
  32. /**
  33. * 该文件夹是否存在
  34. */
  35. + (BOOL)isDirectoryExist:(NSString *)path
  36. {
  37. BOOL isDirectory;
  38. return [[self fileManager] fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory;
  39. }
  40. /**
  41. * 移除该文件
  42. */
  43. + (BOOL)removeFile:(NSString *)path
  44. {
  45. return [[self fileManager] removeItemAtPath:path error:nil];
  46. }
  47. /** 创建目录 */
  48. + (BOOL)createDirectoryAtPath:(NSString *)path
  49. {
  50. return [[self fileManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  51. }
  52. /**
  53. * 文件个数
  54. */
  55. + (NSUInteger)fileCountInPath:(NSString *)path
  56. {
  57. NSUInteger count = 0;
  58. NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
  59. for (__unused NSString *fileName in fileEnumerator) {
  60. count += 1;
  61. }
  62. return count;
  63. }
  64. /**
  65. * 目录大小
  66. */
  67. + (unsigned long long)folderSizeAtPath:(NSString *)path
  68. {
  69. __block unsigned long long totalFileSize = 0;
  70. NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
  71. for (NSString *fileName in fileEnumerator) {
  72. NSString *filePath = [path stringByAppendingPathComponent:fileName];
  73. NSDictionary *fileAttrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
  74. totalFileSize += fileAttrs.fileSize;
  75. }
  76. return totalFileSize;
  77. }
  78. #pragma mark User directory methods
  79. /**
  80. * 应用文件路径
  81. */
  82. + (NSString *)appDocumentDirectoryPath
  83. {
  84. return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  85. }
  86. /**
  87. * 应用资源路径
  88. */
  89. + (NSString *)appResourcePath
  90. {
  91. return [[NSBundle mainBundle] resourcePath];
  92. }
  93. /**
  94. * 应用缓存路径
  95. */
  96. + (NSString *)appCachesDirectoryPath
  97. {
  98. return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  99. }
  100. @end