RQFileManager.m 2.4 KB

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