• 计算沙盒下文件夹内容大小 清空沙盒 文件 目录


     1 +(float)fileSizeForDir:(NSString*)path//计算文件夹下文件的总大小
     2 
     3 {
     4 
     5     NSFileManager *fileManager = [[NSFileManager alloc] init];
     6 
     7     float size =0;
     8 
     9     NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];
    10 
    11     for(int i = 0; i<[array count]; i++)
    12 
    13     {
    14 
    15         NSString *fullPath = [path stringByAppendingPathComponent:[array objectAtIndex:i]];
    16 
    17         
    18 
    19         BOOL isDir;
    20 
    21         if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) )
    22 
    23         {
    24 
    25             NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPath error:nil];
    26 
    27             size+= fileAttributeDic.fileSize/ 1024.0/1024.0;
    28 
    29         }
    30 
    31         else
    32 
    33         {
    34 
    35             [self fileSizeForDir:fullPath];
    36 
    37         }
    38 
    39     }
    40 
    41     [fileManager release];
    42 
    43     return size;
    44 
    45 }

     

    SandBox,沙盒机制,是一种安全体系。我们所开发的每一个应用程序在设备上会有一个对应的沙盒文件夹,当前的程序只能在自己的沙盒文件夹中读取文件,不能访问其他应用程序的沙盒。在项目中添加的所有非代码的资源,比如图片、声音、属性列表等都存在自己的沙盒中。此外,在程序运行中动态生成的或者从网络获取的数据,如果要存储,也都是存储到沙盒中。

    沙盒中的默认文件夹

    (1)Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录

    (2)Library:存储程序的默认设置或其它状态信息;

    里面又包含两个文件夹Caches和Preference;

    Caches,存放缓存文件,iTunes不会备份此目录

    (3)tmp:提供一个即时创建临时文件的地方

    获取沙盒中的不同目录

      1 代码
      2 
      3 //  JRSandBoxPath.h
      4 //  Fmdb
      5 //
      6 //  Created by jerei on 15-10-30.
      7 //  Copyright (c) 2015年 jerei. All rights reserved.
      8 //
      9 #import <Foundation/Foundation.h>
     10 @interface JRSandBoxPath: NSObject
     11 // 获取沙盒Document的文件目录
     12 + (NSString *)getDocumentDirectory;
     13 // 获取沙盒Library的文件目录
     14 + (NSString *)getLibraryDirectory;
     15 // 获取沙盒Library/Caches的文件目录
     16 + (NSString *)getCachesDirectory;
     17 // 获取沙盒Preference的文件目录
     18 + (NSString *)getPreferencePanesDirectory;
     19 // 获取沙盒tmp的文件目录
     20 + (NSString *)getTmpDirectory;
     21 @end
     22 //
     23 //  JRSandBoxPath.m
     24 //  Fmdb
     25 //
     26 //  Created by jerei on 15-10-30.
     27 //  Copyright (c) 2015年 jerei. All rights reserved.
     28 //
     29 #import " JRSandBoxPath.h"
     30 @implementation JRSandBoxPath
     31 #pragma mark - 获取沙盒Document的文件目录
     32 + (NSString *)getDocumentDirectory{
     33   return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
     34 }
     35 #pragma mark - 获取沙盒Library的文件目录
     36 + (NSString *)getLibraryDirectory{
     37   return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
     38 }
     39 #pragma mark - 获取沙盒Library/Caches的文件目录
     40 + (NSString *)getCachesDirectory{
     41   return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
     42 }
     43 #pragma mark - 获取沙盒Preference的文件目录
     44 + (NSString *)getPreferencePanesDirectory{
     45   return [NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES) lastObject];
     46 }
     47 #pragma mark - 获取沙盒tmp的文件目录
     48 + (NSString *)getTmpDirectory{
     49   return
     50   NSTemporaryDirectory();
     51 }
     52 @end
     53 清除缓存
     54 
     55 在开发的过程中,遇到有用的数据,会进行缓存,当该数据不需要时,可以清除。在这里整理了几个方法,统计问价的大小,清除指定文件,清除指定目录下的全部文件等。
     56 
     57 代码
     58 
     59 //  JRCleanCaches.h
     60 //  Fmdb
     61 //
     62 //  Created by jerei on 15-10-30.
     63 //  Copyright (c) 2015年 jerei. All rights reserved.
     64 //
     65 #import <Foundation/Foundation.h>
     66 @interface JRCleanCaches : NSObject
     67 // 根据路径返回目录或文件的大小
     68 + (double)sizeWithFilePath:(NSString *)path;
     69 // 得到指定目录下的所有文件
     70 + (NSArray *)getAllFileNames:(NSString *)dirPath;
     71 // 删除指定目录或文件
     72 + (BOOL)clearCachesWithFilePath:(NSString *)path;
     73 // 清空指定目录下文件
     74 + (BOOL)clearCachesFromDirectoryPath:(NSString *)dirPath;
     75 @end
     76 //
     77 //  JRCleanCaches.m
     78 //  Fmdb
     79 //
     80 //  Created by jerei on 15-10-30.
     81 //  Copyright (c) 2015年 jerei. All rights reserved.
     82 //
     83 #import "JRCleanCaches.h"
     84 @implementation JRCleanCaches
     85 #pragma mark - 根据路径返回目录或文件的大小
     86 + (double)sizeWithFilePath:(NSString *)path{
     87   // 1.获得文件夹管理者
     88   NSFileManager *manger = [NSFileManager defaultManager];
     89   // 2.检测路径的合理性
     90   BOOL dir = NO;
     91   BOOL exits = [manger fileExistsAtPath:path isDirectory:&dir];
     92   if (!exits) return 0;
     93   // 3.判断是否为文件夹
     94   if (dir) { // 文件夹, 遍历文件夹里面的所有文件
     95     // 这个方法能获得这个文件夹下面的所有子路径(直接间接子路径)
     96     NSArray *subpaths = [manger subpathsAtPath:path];
     97     int totalSize = 0;
     98     for (NSString *subpath in subpaths) {
     99       NSString *fullsubpath = [path stringByAppendingPathComponent:subpath];
    100       BOOL dir = NO;
    101       [manger fileExistsAtPath:fullsubpath isDirectory:&dir];
    102       if (!dir) { // 子路径是个文件
    103         NSDictionary *attrs = [manger attributesOfItemAtPath:fullsubpath error:nil];
    104         totalSize += [attrs[NSFileSize] intValue];
    105       }
    106     }
    107     return totalSize / (1024 * 1024.0);
    108   } else { // 文件
    109     NSDictionary *attrs = [manger attributesOfItemAtPath:path error:nil];
    110     return [attrs[NSFileSize] intValue] / (1024.0 * 1024.0);
    111   }
    112 }
    113 #pragma mark - 得到指定目录下的所有文件
    114 + (NSArray *)getAllFileNames:(NSString *)dirPath{
    115   NSArray *files = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:dirPath error:nil];
    116   return files;
    117 }
    118 #pragma mark - 删除指定目录或文件
    119 + (BOOL)clearCachesWithFilePath:(NSString *)path{
    120   NSFileManager *mgr = [NSFileManager defaultManager];
    121   return [mgr removeItemAtPath:path error:nil];
    122 }
    123 #pragma mark - 清空指定目录下文件
    124 + (BOOL)clearCachesFromDirectoryPath:(NSString *)dirPath{
    125   //获得全部文件数组
    126   NSArray *fileAry =  [JRCleanCaches getAllFileNames:dirPath];
    127   //遍历数组
    128   BOOL flag = NO;
    129   for (NSString *fileName in fileAry) {
    130     NSString *filePath = [dirPath stringByAppendingPathComponent:fileName];
    131     flag = [JRCleanCaches clearCachesWithFilePath:filePath];
    132     if (!flag)
    133       break;
    134   }
    135   return flag;
    136 }
    137 @end
    让明天,不后悔今天的所作所为
  • 相关阅读:
    EF数据迁移完整步骤
    ajax跨域最全解决方案
    WPF控件与WPF窗体
    WPF模板是把控件MVC模式化
    对象与类型
    Java加权负载均衡策略
    db2列式存储
    linux离线安装mongodb及java调用
    python合并目录下excel数据
    python多线程迁移db2数仓9T数据
  • 原文地址:https://www.cnblogs.com/-yun/p/5090181.html
Copyright © 2020-2023  润新知