oc 根據文件路徑獲取文件大小


第一種封裝:

-(NSInteger)getSizeOfFilePath:(NSString *)filePath{
    /** 定義記錄大小 */
    NSInteger totalSize = 0;
    /** 創建一個文件管理對象 */
    NSFileManager * manager = [NSFileManager defaultManager];
    /**獲取文件下的所有路徑包括子路徑 */
    NSArray * subPaths = [manager subpathsAtPath:filePath];
    /** 遍歷獲取文件名稱 */
    for (NSString * fileName in subPaths) {
        /** 拼接獲取完整路徑 */
        NSString * subPath = [filePath stringByAppendingPathComponent:fileName];
        /** 判斷是否是隱藏文件 */
        if ([fileName hasPrefix:@".DS"]) {
            continue;
        }
        /** 判斷是否是文件夾 */
        BOOL isDirectory;
        [manager fileExistsAtPath:subPath isDirectory:&isDirectory];
        if (isDirectory) {
            continue;
        }
        /** 獲取文件屬性 */
        NSDictionary *dict = [manager attributesOfItemAtPath:subPath error:nil];
        /** 累加 */
        totalSize += [dict fileSize];
        
    }
    /** 返回 */
    return totalSize;
}

第二種  block:

/** 根據文件路徑刪除文件 */
+(void)removeDirectoryPath:(NSString *)directoryPath{

    /** 創建文件管理者 */
    NSFileManager * manager = [NSFileManager defaultManager];
    /** 判斷文件路徑是否存在 */
    BOOL isDirectory;
    BOOL isExist = [manager fileExistsAtPath:directoryPath isDirectory:&isDirectory];
    if (!isDirectory||!isExist) {
        /** 提示錯誤信息 */
        @throw [NSException exceptionWithName:NSStringFromClass(self) reason:@"文件路徑錯誤!" userInfo:nil];
    }
    /** 刪除文件 */
    [manager removeItemAtPath:directoryPath error:nil];
    /** 創建文件 */
    [manager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
}
/** 根據文件路徑獲取文件大小 */
+(void)getSizeOfFilePath:(NSString *)filePath completion:(void (^)(NSInteger totalSize))completion{
    
    /** 創建文件管理者 */
    NSFileManager * manager = [NSFileManager defaultManager];
    /** 判斷文件路徑是否存在 */
    BOOL isDirectory;
    BOOL isExist = [manager fileExistsAtPath:filePath isDirectory:&isDirectory];
    if (!isDirectory||!isExist) {
        /** 提示錯誤信息 */
        @throw [NSException exceptionWithName:NSStringFromClass(self) reason:@"文件路徑錯誤!" userInfo:nil];
    }
    /** 開啟子線程因以下是耗時操作 */
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        /** 定義記錄文件大小 */
        NSInteger totalSize = 0;
        
        /** 獲取文件 */
        NSArray * subPaths = [manager subpathsAtPath:filePath];
        /** 遍歷文件 */
        for (NSString * fileName in subPaths) {
            /** 拼接完整路徑 */
            NSString * subPath = [filePath stringByAppendingPathComponent:fileName];
            /** 判斷是否是隱藏.DS_Store */
            if ([subPath hasPrefix:@".DS"]) {
                continue;
            }
            /** 判斷是否是文件夾 */
            BOOL isDirectory;
            [manager fileExistsAtPath:subPath isDirectory:&isDirectory];
            if (isDirectory) {
                continue;
            }
            /** 獲取文件屬性 */
            NSDictionary * dic = [manager attributesOfItemAtPath:subPath error:nil];
            totalSize += [dic fileSize];
        }
         /** 回到主線程 */
        dispatch_sync(dispatch_get_main_queue(), ^{
            /** block不為空 */
            if (completion) {
                completion(totalSize);
            }
        });
    });
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM