iOS - 获取文件、文件夹大小

//单个文件的大小

- (long long) fileSizeAtPath:(NSString*) filePath{

    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;

}

//遍历文件夹获得文件夹大小

- (NSString *) folderSizeAtPath:(NSString*) folderPath{

    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    if (folderSize < 1024.0) {
        return  [NSString stringWithFormat:@"%.2fB",folderSize * 1.0];
    }else if (folderSize >= 1024.0 && folderSize < (1024.0*1024.0)){
        return  [NSString stringWithFormat:@"%.2fKB",folderSize/1024.0];
    }if (folderSize >= (1024.0*1024.0) && folderSize < (1024.0*1024.0*1024.0)) {
        return [NSString stringWithFormat:@"%.2fMB", folderSize/(1024.0*1024.0)];
    }else{
        return [NSString stringWithFormat:@"%.2fGB", folderSize/(1024.0*1024.0*1024.0)];
    }
}
        NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:npath traverseLink:YES];
        NSLog(@"@@");
        if (fileAttributes != nil) {
            NSNumber *fileSize;
            NSString *fileOwner, *creationDate;
            NSDate *fileModDate;
            //NSString *NSFileCreationDate
            //文件大小
            if (fileSize == [fileAttributes objectForKey:NSFileSize]) {
                NSLog(@"File size: %qi\n", [fileSize unsignedLongLongValue]);
            }
            //文件创建日期
            if ((creationDate = [fileAttributes objectForKey:NSFileCreationDate])) {
                NSLog(@"File creationDate: %@\n", creationDate);
                //textField.text=NSFileCreationDate;
            }
            //文件所有者
            if ((fileOwner = [fileAttributes objectForKey:NSFileOwnerAccountName])) {
                NSLog(@"Owner: %@\n", fileOwner);
            }
            //文件修改日期
            if ((fileModDate = [fileAttributes objectForKey:NSFileModificationDate])) {
                NSLog(@"Modification date: %@\n", fileModDate);
            }
        }
        else {
            NSLog(@"Path (%@) is invalid.", npath);
        }

最终整理代码

//单个文件的大小

long long fileSizeAtPath(NSString* filePath){

    NSFileManager* manager = [NSFileManager defaultManager];
    
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

//遍历文件夹获得文件夹大小

NSString *folderSizeAtPath(NSString* folderPath){
    
    NSFileManager* manager = [NSFileManager defaultManager];
    
    if (![manager fileExistsAtPath:folderPath]) return 0;
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    
    NSString* fileName;
    
    long long folderSize = 0;
    
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        
        folderSize += fileSizeAtPath(fileAbsolutePath);
        
    }
    return  sizeOfFile(folderSize);
}

// 返回具体的大小字符串

NSString *sizeOfFile(long long folderSize){

    if (folderSize < 1024.0) {
        return  [NSString stringWithFormat:@"%.2fB",folderSize * 1.0];
    }else if (folderSize >= 1024.0 && folderSize < (1024.0*1024.0)){
        return  [NSString stringWithFormat:@"%.2fKB",folderSize/1024.0];
    }if (folderSize >= (1024.0*1024.0) && folderSize < (1024.0*1024.0*1024.0)) {
        return [NSString stringWithFormat:@"%.2fMB", folderSize/(1024.0*1024.0)];
    }else{
        return [NSString stringWithFormat:@"%.2fGB", folderSize/(1024.0*1024.0*1024.0)];
    }
}

你可能感兴趣的:(iOS - 获取文件、文件夹大小)