iOS 獲取文件屬性 NSDirectoryEnumerator 文件大小

 //如果沒有目錄則創建目錄

    [[NSFileManager defaultManager]createDirectoryAtPath:[kDocumentsPath stringByAppendingPathComponent:@"PhotoImg"] withIntermediateDirectories:YES attributes:nil error:nil];

    //遞歸枚舉目錄

    NSString *dir = [NSString stringWithFormat:@"%@/PhotoImg",kDocumentsPath];

    NSDirectoryEnumerator *dirEnumerater = [[NSFileManager defaultManager] enumeratorAtPath:dir];

    NSString *filePath = nil;

    

    NSDictionary *arrtibutes = [[NSFileManager defaultManager]attributesOfItemAtPath:dir error:nil];

    

    NSNumber *theFileSize ;

    if ((theFileSize = [arrtibutes objectForKey:NSFileSize])) {

        NSLog(@"%@",theFileSize);

    }

    

    NSLog(@"%@",[dirEnumerater fileAttributes]);

  

    int fileSizeReal = getFileSizeFormPath((char *)[dir UTF8String]);

    NSLog(@"%d",fileSizeReal);


int getFileSizeFormPath(char *path){

    FILE * file;

    int fileSizeByte = 0;

    file = fopen(path, "r");

    if (file>0) {

        fseek(file, 0, SEEK_END);

        fileSizeByte = ftell(file);

        fseek(file, 0, SEEK_SET);

        fclose(file);

    }

    return fileSizeByte;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章