ZFileManagerHelper.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // ZFileManagerHelper.m
  3. // XPH
  4. //
  5. // Created by Zick.Zhao on 15/3/17.
  6. // Copyright (c) 2015年 YAY. All rights reserved.
  7. //
  8. #import "ZFileManagerHelper.h"
  9. @implementation ZFileManagerHelper
  10. #pragma mark FileManager Methoed
  11. //获取Cache目录路径
  12. +(NSString*)dirCache{
  13. NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  14. NSString *cachePath = [cacPath objectAtIndex:0];
  15. return cachePath;
  16. }
  17. //创建文件夹
  18. +(BOOL)xCreateCacheDirWithPath:(NSString*)_path{
  19. NSString *cachePath =[self dirCache];
  20. cachePath = [cachePath stringByAppendingPathComponent:_path];
  21. BOOL isDir = FALSE;
  22. BOOL isExists = [zFileManager fileExistsAtPath:cachePath isDirectory:&isDir];
  23. // 创建目录
  24. if (!(isExists && isDir)) {
  25. BOOL res =[zFileManager createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:nil];
  26. if (res) {
  27. NSLog(@"%@文件夹创建成功",_path);
  28. return YES;
  29. }else{
  30. NSLog(@"%@文件夹创建失败",_path);
  31. return NO;
  32. }
  33. }
  34. else{
  35. NSLog(@"%@文件夹已存在",_path);
  36. return YES;
  37. }
  38. }
  39. //判断文件夹是否存在
  40. + (BOOL) xCheckDirectoryIsExisit : (NSString *) _dirPath{
  41. // 判断存放音频、视频的文件夹是否存在,不存在则创建对应文件夹
  42. BOOL isDir = FALSE;
  43. BOOL isDirExist = [zFileManager fileExistsAtPath:_dirPath isDirectory:&isDir];
  44. return isDirExist;
  45. }
  46. + (BOOL)xCheckFileIsExists:(NSString *)_path{
  47. if([zFileManager fileExistsAtPath:_path]){
  48. return YES;
  49. }else{
  50. return NO;
  51. }
  52. }
  53. ////检查文件是否存在
  54. //- (BOOL) CBC_CheckFileIsExisit : (NSString *) _filePath
  55. //{
  56. // if (!FileManager)
  57. // {
  58. // FileManager = [NSFileManager defaultManager];
  59. // }
  60. //
  61. // NSString *filePath = [self xGetFilePathWithFileUnderDocumentPath:_filePath];
  62. //
  63. // if([FileManager fileExistsAtPath:filePath])
  64. // {
  65. // return YES;
  66. // }
  67. // return NO;
  68. //}
  69. //
  70. ////获取Cache文件夹下面的文件路径
  71. //- (NSString *) xGetFilePathWithFileUnderDocumentPath : (NSString *) _filePath
  72. //{
  73. // NSString *fileTurePath = @"";
  74. // NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
  75. // NSString *docDirectoryStr = [pathArr objectAtIndex:0];
  76. // fileTurePath = [docDirectoryStr stringByAppendingPathComponent:_filePath];
  77. // return fileTurePath;
  78. //}
  79. //
  80. ////获取Doc文件夹下面的文件路径
  81. //- (NSString *) xGetFilePathWithFileUnderDocumentPathArr : (NSArray *) _filePathArr
  82. //{
  83. // NSString *filePath = @"";
  84. // for (int i = 0; i < [_filePathArr count]; i++)
  85. // {
  86. // if (filePath.length > 0)
  87. // {
  88. // filePath = [filePath stringByAppendingPathComponent:[_filePathArr objectAtIndex:i]];
  89. // }
  90. // else
  91. // {
  92. // filePath = [_filePathArr objectAtIndex:i];
  93. // }
  94. // }
  95. // return filePath;
  96. //}
  97. //
  98. //#pragma mark --------------------------------------------------------------------
  99. //
  100. ////创建UnicomJKZY目录结构
  101. //- (BOOL) xCreateUnicomJKZYDirectoryCacheStructure
  102. //{
  103. // BOOL create_result = NO;
  104. //
  105. // create_result = [self CBC_CreateCommonCacheDirectory];
  106. // create_result &= [self CBC_CreateUserCacheDirectory];
  107. //
  108. // return create_result;
  109. //}
  110. //
  111. ////创建公用文件夹
  112. //- (BOOL) xCreateCommonCacheDirectory
  113. //{
  114. // return [self CBC_CreateDirectoryWithDirName:Cache_Common];
  115. //}
  116. //
  117. ////创建文件夹
  118. //- (BOOL) xCreateDirectoryWithDirName : (NSString *) _dirName
  119. //{
  120. // return [self xCheckDirectoryIsExisit:_dirName];
  121. //}
  122. //
  123. ////创建用户数据目录结构
  124. //- (BOOL) CBC_CreateUserCacheDirectory
  125. //{
  126. // NSString *dir_tree_path = Cache_Users;
  127. // BOOL create_result;
  128. //
  129. // create_result = [self CBC_CreateDirectoryWithDirName:dir_tree_path];
  130. //
  131. // if (create_result)
  132. // {
  133. // dir_tree_path = [self CBC_GetFilePathWithFileUnderDocumentPathArr:
  134. // [[NSArray alloc] initWithObjects:
  135. // dir_tree_path,
  136. // CURR_RES_USERNAME,
  137. // nil]];
  138. //
  139. // create_result = [self CBC_CreateDirectoryWithDirName:dir_tree_path];
  140. //
  141. // }
  142. // return create_result;
  143. //}
  144. //
  145. ////删除用户数据目录结构
  146. //- (void) CBC_DeleteNotCurrentUserCacheDirectory
  147. //{
  148. // NSString *path = [[Cache_BaseControl Instance] CBC_GetFilePathWithFileUnderDocumentPath:Cache_Users];
  149. //
  150. // NSArray *userArr = [FileManager directoryContentsAtPath:path];
  151. //
  152. // NSMutableArray *deleteArr = [[NSMutableArray alloc] init];
  153. //
  154. // for (NSString *subStr in userArr)
  155. // {
  156. // if(![CURR_RES_USERNAME isEqualToString:subStr])
  157. // {
  158. // [deleteArr addObject:[[Cache_Users stringByAppendingString:@"/"] stringByAppendingString:subStr]];
  159. // }
  160. // }
  161. //
  162. // if ([deleteArr count] > 0)
  163. // {
  164. // [self CBC_DeleteFileWithFilePathArr:deleteArr];
  165. // }
  166. //
  167. //}
  168. //
  169. ////根据文件路径删除文件
  170. //- (void) CBC_DeleteFileWithFilePathArr : (NSArray *) _filePathArr
  171. //{
  172. // NSError *error;
  173. // for (int i = 0; i < [_filePathArr count]; i++)
  174. // {
  175. // NSString *filePathStr = [self CBC_GetFilePathWithFileUnderDocumentPath:(NSString *)[_filePathArr objectAtIndex:i]];
  176. // if([FileManager fileExistsAtPath:filePathStr])
  177. // {
  178. // if ([FileManager removeItemAtPath:filePathStr error:&error] != YES)
  179. // {
  180. // NSLog(@"Unable to delete file: %@", [error localizedDescription]);
  181. // }
  182. // else
  183. // {
  184. // NSLog(@"delete %@ success!",filePathStr);
  185. // }
  186. // }
  187. // else
  188. // {
  189. // NSLog(@"not exisit %@ file",filePathStr);
  190. // }
  191. // }
  192. //}
  193. #pragma mark cacheMethod
  194. + (long long) fileSizeAtPath:(NSString*) filePath{
  195. NSFileManager* manager = [NSFileManager defaultManager];
  196. if ([manager fileExistsAtPath:filePath]){
  197. return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
  198. }
  199. return 0;
  200. }
  201. // 循环调用fileSizeAtPath来获取一个目录所占空间大小
  202. + (NSString*) getCacheSize{
  203. NSString *folderPath = [ZFileManagerHelper getCachePath];
  204. NSFileManager* manager = [NSFileManager defaultManager];
  205. if (![manager fileExistsAtPath:folderPath]) return 0;
  206. NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
  207. NSString* fileName;
  208. long long folderSize = 0;
  209. while ((fileName = [childFilesEnumerator nextObject]) != nil){
  210. NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
  211. folderSize += [self fileSizeAtPath:fileAbsolutePath];
  212. }
  213. CGFloat value = (float)folderSize;
  214. if ( folderSize > 1024 * 1024) {
  215. value = value / (1024 * 1024);
  216. return [NSString stringWithFormat:@"%.2f M",value];
  217. }
  218. else{
  219. value = value / 1024 ;
  220. return [NSString stringWithFormat:@"%.2f K",value];
  221. }
  222. return nil;
  223. }
  224. +(NSString *)getCachePath{
  225. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  226. // NSString *documentsDirectory = [paths objectAtIndex:0];//去处需要的路径
  227. // NSString *path = [documentsDirectory stringByAppendingFormat:@"/Cache/"];
  228. //
  229. // if(self.hCacheDictionaryPath){
  230. // path = [path stringByAppendingFormat:@"%@/",self.hCacheDictionaryPath];
  231. // }
  232. return [ZFileManagerHelper dirCache];
  233. }
  234. + (BOOL)cleanCache{
  235. [ZFileManagerHelper flushCache];
  236. NSString *dir = [ZFileManagerHelper getCachePath];
  237. NSArray *contents = [zFileManager contentsOfDirectoryAtPath:dir error:NULL];
  238. NSEnumerator *e = [contents objectEnumerator];
  239. NSString *filename;
  240. BOOL returnFlag = false;
  241. while ((filename = [e nextObject])) {
  242. returnFlag = [zFileManager removeItemAtPath:[dir stringByAppendingPathComponent:filename] error:NULL];
  243. }
  244. return returnFlag;
  245. }
  246. + (void)flushCache
  247. {
  248. [SDWebImageManager.sharedManager.imageCache clearMemory];
  249. [SDWebImageManager.sharedManager.imageCache clearDiskOnCompletion:nil];
  250. }
  251. @end