123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- //
- // RQFileManager.m
- // RQCommon
- //
- // Created by 张嵘 on 2018/11/21.
- // Copyright © 2018 张嵘. All rights reserved.
- //
- #import "RQFileManager.h"
- @implementation RQFileManager
- /**
- * 文件管理器
- */
- + (NSFileManager *)fileManager
- {
- return [NSFileManager defaultManager];
- }
- /**
- * 该路径是否存在
- */
- + (BOOL)isPathExist:(NSString *)path
- {
- return [[self fileManager] fileExistsAtPath:path];
- }
- /**
- * 该文件是否存在
- */
- + (BOOL)isFileExist:(NSString *)path
- {
- BOOL isDirectory;
- return [[self fileManager] fileExistsAtPath:path isDirectory:&isDirectory] && !isDirectory;
- }
- /**
- * 该文件夹是否存在
- */
- + (BOOL)isDirectoryExist:(NSString *)path
- {
- BOOL isDirectory;
- return [[self fileManager] fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory;
- }
- /**
- * 移除该文件
- */
- + (BOOL)removeFile:(NSString *)path
- {
- return [[self fileManager] removeItemAtPath:path error:nil];
- }
- /** 创建目录 */
- + (BOOL)createDirectoryAtPath:(NSString *)path
- {
- return [[self fileManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
- }
- /**
- * 文件个数
- */
- + (NSUInteger)fileCountInPath:(NSString *)path
- {
- NSUInteger count = 0;
- NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
- for (__unused NSString *fileName in fileEnumerator) {
- count += 1;
- }
- return count;
- }
- /**
- * 目录大小
- */
- + (unsigned long long)folderSizeAtPath:(NSString *)path
- {
- __block unsigned long long totalFileSize = 0;
- NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
- for (NSString *fileName in fileEnumerator) {
- NSString *filePath = [path stringByAppendingPathComponent:fileName];
- NSDictionary *fileAttrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
- totalFileSize += fileAttrs.fileSize;
- }
- return totalFileSize;
- }
- #pragma mark User directory methods
- /**
- * 应用文件路径
- */
- + (NSString *)appDocumentDirectoryPath
- {
- return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
- }
- /**
- * 应用资源路径
- */
- + (NSString *)appResourcePath
- {
- return [[NSBundle mainBundle] resourcePath];
- }
- /**
- * 应用缓存路径
- */
- + (NSString *)appCachesDirectoryPath
- {
- return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
- }
- @end
|