PathUtility.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. //
  2. // PathUtility.m
  3. //
  4. //
  5. // Created by Alexi on 12-11-4.
  6. // Copyright (c) 2012年 . All rights reserved.
  7. //
  8. #import "PathUtility.h"
  9. #import "NSString+Common.h"
  10. @implementation PathUtility
  11. + (NSString *)getFileDocumentPath:(NSString *)fileName
  12. {
  13. if (nil == fileName)
  14. {
  15. return nil;
  16. }
  17. NSString *documentDirectory = [PathUtility getDocumentPath];
  18. NSString *fileFullPath = [documentDirectory stringByAppendingPathComponent:fileName];
  19. return fileFullPath;
  20. }
  21. + (NSString *)getFileCachePath:(NSString *)fileName
  22. {
  23. if (nil == fileName)
  24. {
  25. return nil;
  26. }
  27. NSString *cacheDirectory = [PathUtility getCachePath];
  28. NSString *fileFullPath = [cacheDirectory stringByAppendingPathComponent:fileName];
  29. return fileFullPath;
  30. }
  31. + (NSString *)getFileResourcePath:(NSString *)fileName
  32. {
  33. if ([NSString isEmpty:fileName])
  34. {
  35. return nil;
  36. }
  37. // 获取资源目录路径
  38. NSString *resourceDir = [[NSBundle mainBundle] resourcePath];
  39. return [resourceDir stringByAppendingPathComponent:fileName];
  40. }
  41. + (BOOL)isExistFileInDocument:(NSString *)fileName
  42. {
  43. if ([NSString isEmpty:fileName])
  44. {
  45. return NO;
  46. }
  47. NSString *filePath = [PathUtility getFileDocumentPath:fileName];
  48. if (nil == filePath)
  49. {
  50. return NO;
  51. }
  52. NSFileManager *fileManager = [NSFileManager defaultManager];
  53. return [fileManager fileExistsAtPath:filePath];
  54. }
  55. + (BOOL)isExistFileInCache:(NSString *)fileName
  56. {
  57. if (nil == fileName || [fileName length] == 0)
  58. {
  59. return NO;
  60. }
  61. NSString *filePath = [PathUtility getFileCachePath:fileName];
  62. if (nil == filePath)
  63. {
  64. return NO;
  65. }
  66. NSFileManager *fileManager = [NSFileManager defaultManager];
  67. return [fileManager fileExistsAtPath:filePath];
  68. }
  69. + (BOOL)removeFolderInDocumet:(NSString *)aFolderNameInDoc
  70. {
  71. if ([NSString isEmpty:aFolderNameInDoc])
  72. {
  73. return YES ;
  74. }
  75. NSString *filePath = [PathUtility getFileDocumentPath:aFolderNameInDoc];
  76. if (nil == filePath)
  77. {
  78. return YES;
  79. }
  80. NSFileManager *fileManager = [NSFileManager defaultManager];
  81. return [fileManager removeItemAtPath:filePath error:nil];
  82. }
  83. + (BOOL)removeFolderInCahe:(NSString *)aFolderNameInCahe
  84. {
  85. if ([NSString isEmpty:aFolderNameInCahe])
  86. {
  87. return YES ;
  88. }
  89. if (![PathUtility isExistFileInCache:aFolderNameInCahe]) {
  90. return YES;
  91. }
  92. NSString *filePath = [PathUtility getFileCachePath:aFolderNameInCahe];
  93. if (nil == filePath)
  94. {
  95. return YES;
  96. }
  97. NSFileManager *fileManager = [NSFileManager defaultManager];
  98. return [fileManager removeItemAtPath:filePath error:nil];
  99. }
  100. //+ (BOOL)removeComicBookFolder:(NSInteger)bookId
  101. //{
  102. // NSString *filePath = [PathUtility getComicBookDir:bookId];
  103. // if (nil == filePath)
  104. // {
  105. // return YES;
  106. // }
  107. // NSFileManager *fileManager = [NSFileManager defaultManager];
  108. // return [fileManager removeItemAtPath:filePath error:nil];
  109. //}
  110. // 判断一个文件是否存在于resource目录下
  111. + (BOOL)isExistFileInResource:(NSString *)fileName
  112. {
  113. if ([NSString isEmpty:fileName])
  114. {
  115. return NO;
  116. }
  117. NSString *filePath = [PathUtility getFileResourcePath:fileName];
  118. if (nil == filePath)
  119. {
  120. return NO;
  121. }
  122. NSFileManager *fileManager = [NSFileManager defaultManager];
  123. return [fileManager fileExistsAtPath:filePath];
  124. }
  125. + (BOOL)isExistFile:(NSString *)aFilePath
  126. {
  127. if ([NSString isEmpty:aFilePath])
  128. {
  129. return NO;
  130. }
  131. NSFileManager *fileManager = [NSFileManager defaultManager];
  132. return [fileManager fileExistsAtPath:aFilePath];
  133. }
  134. + (BOOL)copyResourceFileToDocumentPath:(NSString *)resourceName
  135. {
  136. if ([NSString isEmpty:resourceName])
  137. {
  138. return NO;
  139. }
  140. //获取资源文件的存放目录进行
  141. NSString *resourcePath = [PathUtility getFileResourcePath:resourceName];
  142. NSString *documentPath = [PathUtility getFileDocumentPath:resourceName];
  143. if (nil == resourcePath || nil == documentPath)
  144. {
  145. return NO;
  146. }
  147. NSFileManager *fileManager = [NSFileManager defaultManager];
  148. if ([PathUtility isExistFile:documentPath])
  149. {
  150. // 如果文件已存在, 那么先删除原来的
  151. [PathUtility deleteFileAtPath:documentPath];
  152. }
  153. BOOL succ = [fileManager copyItemAtPath:resourcePath toPath:documentPath error:nil];
  154. return succ;
  155. }
  156. + (BOOL)deleteFileAtPath:(NSString *)filePath
  157. {
  158. if ([NSString isEmpty:filePath])
  159. {
  160. return NO;
  161. }
  162. // 判断文件是否存在
  163. NSFileManager *fileManager = [NSFileManager defaultManager];
  164. if ([fileManager fileExistsAtPath:filePath])
  165. {
  166. return [fileManager removeItemAtPath:filePath error:nil];
  167. }
  168. DebugLog(@"删除的文件不存在");
  169. return NO;
  170. }
  171. + (NSDictionary *)getFileAttributsAtPath:(NSString *)filePath
  172. {
  173. if ([NSString isEmpty:filePath])
  174. {
  175. return nil;
  176. }
  177. NSFileManager *fileManager = [NSFileManager defaultManager];
  178. if ([fileManager fileExistsAtPath:filePath] == NO)
  179. {
  180. return nil;
  181. }
  182. return [fileManager attributesOfItemAtPath:filePath error:nil];
  183. }
  184. + (BOOL)createDirectoryAtDocument:(NSString *)dirName
  185. {
  186. if (nil == dirName)
  187. {
  188. return NO;
  189. }
  190. NSFileManager *fileManager = [NSFileManager defaultManager];
  191. NSString *dirPath = [PathUtility getFileDocumentPath:dirName];
  192. if ([fileManager fileExistsAtPath:dirPath])
  193. {
  194. return YES;
  195. }
  196. BOOL succ = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];
  197. return succ;
  198. }
  199. + (BOOL)createDirectoryAtCache:(NSString *)dirName
  200. {
  201. if (nil == dirName)
  202. {
  203. return NO;
  204. }
  205. NSFileManager *fileManager = [NSFileManager defaultManager];
  206. NSString *dirPath = [PathUtility getFileCachePath:dirName];
  207. if ([fileManager fileExistsAtPath:dirPath])
  208. {
  209. return YES;
  210. }
  211. BOOL succ = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];
  212. return succ;
  213. }
  214. + (BOOL)createDirectoryAtTemporary:(NSString *)dirName
  215. {
  216. if (nil == dirName)
  217. {
  218. return NO;
  219. }
  220. NSFileManager *fileManager = [NSFileManager defaultManager];
  221. NSString *tempPath = [PathUtility getTemporaryPath];
  222. NSString *dirPath = [NSString stringWithFormat:@"%@/%@", tempPath, dirName];
  223. if ([fileManager fileExistsAtPath:dirPath])
  224. {
  225. return YES;
  226. }
  227. BOOL succ = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];
  228. return succ;
  229. }
  230. // 获取文档目录路径
  231. + (NSString *)getDocumentPath
  232. {
  233. // 获取文档目录路径
  234. NSArray *userPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  235. return [userPaths objectAtIndex:0];
  236. }
  237. // 获取cache目录路径
  238. + (NSString *)getCachePath
  239. {
  240. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  241. return [paths objectAtIndex:0];
  242. }
  243. + (NSString *)getTemporaryPath
  244. {
  245. return NSTemporaryDirectory();
  246. }
  247. + (long long)getFreeSpaceOfDisk
  248. {
  249. NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
  250. NSNumber *freeSpace = [fattributes objectForKey:NSFileSystemFreeSize];
  251. long long space = [freeSpace longLongValue];
  252. return space;
  253. }
  254. + (long long)getFileSize:(NSString *)filePath
  255. {
  256. NSDictionary *fileAttributes = [self getFileAttributsAtPath:filePath];
  257. if (fileAttributes)
  258. {
  259. NSNumber *fileSize = (NSNumber*)[fileAttributes objectForKey: NSFileSize];
  260. if (fileSize)
  261. {
  262. return [fileSize longLongValue];
  263. }
  264. }
  265. return 0;
  266. }
  267. + (BOOL)copySourceFile:(NSString *)sourceFile toDesPath:(NSString *)desPath
  268. {
  269. DebugLog(@"despath:%@", desPath);
  270. NSFileManager *fileManager = [NSFileManager defaultManager];
  271. // 读取文件的信息
  272. NSData *sourceData = [NSData dataWithContentsOfFile:sourceFile];
  273. BOOL e = NO;
  274. if (sourceData)
  275. {
  276. e = [fileManager createFileAtPath:desPath contents:sourceData attributes:nil];
  277. }
  278. // NSError *error = nil;
  279. // BOOL e = [fileManager copyItemAtPath:sourceFile toPath:desPath error:&error];
  280. if (e)
  281. {
  282. DebugLog(@"copySourceFile成功");
  283. }
  284. else
  285. {
  286. DebugLog(@"copySourceFile失败");
  287. }
  288. return YES;
  289. }
  290. + (BOOL)moveSourceFile:(NSString *)sourceFile toDesPath:(NSString *)desPath
  291. {
  292. NSFileManager *fileManager = [NSFileManager defaultManager];
  293. NSError *error = nil;
  294. [fileManager moveItemAtPath:sourceFile toPath:desPath error:&error];
  295. if (error)
  296. {
  297. return NO;
  298. }
  299. return YES;
  300. }
  301. // 如果应用程序覆盖安装后,其document目录会发生变化,该函数用于替换就的document路径
  302. + (NSString *)reCorrentPathWithPath:(NSString *)path
  303. {
  304. if (nil == path)
  305. {
  306. return nil;
  307. }
  308. NSString *docPath = [PathUtility getDocumentPath];
  309. NSRange range = [path rangeOfString:docPath];
  310. // 没找到正确的document路径
  311. if (range.length <= 0)
  312. {
  313. NSRange docRange = [path rangeOfString:@"Documents/"];
  314. if (docRange.length > 0)
  315. {
  316. NSString *relPath = [path substringFromIndex:docRange.location + docRange.length];
  317. NSString *newPath = [PathUtility getFileDocumentPath:relPath];
  318. return newPath;
  319. }
  320. }
  321. return path;
  322. }
  323. + (unsigned long long int)folderSize:(NSString *)folderPath
  324. {
  325. NSFileManager *mgr = [NSFileManager defaultManager];
  326. NSArray *filesArray = [mgr subpathsOfDirectoryAtPath:folderPath error:nil];
  327. NSEnumerator *filesEnumerator = [filesArray objectEnumerator];
  328. NSString *fileName;
  329. unsigned long long int fileSize = 0;
  330. while (fileName = [filesEnumerator nextObject]) {
  331. NSDictionary *fileDictionary = [mgr attributesOfItemAtPath:[folderPath stringByAppendingPathComponent:fileName] error:nil];
  332. fileSize += [fileDictionary fileSize];
  333. }
  334. return fileSize;
  335. }
  336. + (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
  337. {
  338. if (![[NSFileManager defaultManager] fileExistsAtPath: [URL path]])
  339. {
  340. return NO;
  341. }
  342. NSError *error = nil;
  343. BOOL success = [URL setResourceValue:[NSNumber numberWithBool: YES]
  344. forKey: NSURLIsExcludedFromBackupKey error: &error];
  345. if(!success)
  346. {
  347. NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
  348. }
  349. return success;
  350. }
  351. + (NSString *)imageCachePath
  352. {
  353. return [PathUtility getTemporaryPath];
  354. }
  355. @end