iOS NSFileManeger 计算文件是否超时,和计算文件夹下文件的总大小

2015-05-18 0 676
iOS NSFileManeger 计算文件是否超时,和计算文件夹下文件的总大小
//获得指定文件距离上次修改时间是否达到了指定值(秒)timeout

+(BOOL)isTimeout:(NSString *)path time:(NSTimeInterval)timeout

{

    //获得当前时间

    NSTimeInterval now = [[NSDate date] timeIntervalSince1970];

    

    NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];

    // 取得了文件上次修改的时间

    NSDate *d = [dict objectForKey:NSFileModificationDate];

    if (now-[d timeIntervalSince1970]>timeout) {

        return YES;

    }

    return NO;

}

//计算文件夹下文件的总大小

+(float)fileSizeForDir:(NSString*)path

{

    NSFileManager *fileManager = [NSFileManager defaultManager];

    

    //记录总值

    unsigned long long totalSize =0;

    //获得指定路径path的所有内容(文件和文件夹)

    NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];

    for(int i = 0; i<[array count]; i++)

    {

        //拼接全路径

        NSString *fullPath = [path stringByAppendingPathComponent:[array objectAtIndex:i]];

        BOOL isDir;

        

        //如果指定路径存在并且不是文件夹

        //NSLog(@"fullPath:%@",fullPath);

        //先判断是否存在,再判断是文件夹还是文件

        if ([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && !isDir)

        {

            //获得文件属性

            NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPath error:nil];

            totalSize+=[[fileAttributeDic objectForKey:NSFileSize] unsignedLongLongValue];

        }

        else

        {

            //如果是文件夹,递归

            totalSize+=[self fileSizeForDir:fullPath];

        }

    }

    return totalSize;

}

遇见资源网 c/c++ iOS NSFileManeger 计算文件是否超时,和计算文件夹下文件的总大小 http://www.ox520.com/10085.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务