沙盒文件管理淺析

注:本工具類須要導入SDWebImage的緩存管理類「SDImageCache.h」頭文件 若須要管理其餘三方緩存,將其頭文件導入並做相應處理便可。git

####寫在前面的 該工具類只是對沙盒文件路徑查找、刪除,對文件內容歸檔、解檔,進行了一些簡單的封裝,第一次發文,還望你們多多指點。 bling bling !!! github地址github


#####沙盒查看方法 我知道大家喜歡貼圖黨。。。web

1.xcode菜單欄Window-Devices 屏幕快照 2016-09-23 下午12.02.20.png 2.選擇真機 屏幕快照 2016-09-23 下午12.03.00.png 3.選中右側你要沙盒的app 屏幕快照 2016-09-23 下午12.03.17.png 4.Download Container 屏幕快照 2016-09-23 下午12.03.22.png 5.保存以後文件右擊-顯示包內容 6.方得廬山真面目 屏幕快照 2016-09-23 下午12.01.51.png數組


##大家最愛的xcode

#import@interface LZSandBoxManager : NSObject

#pragma mark - 獲取沙盒目錄 -

/**

獲取沙盒Document目錄

@return Document目錄

*/

+ (NSString *)getDocumentDirectory;

/**

獲取沙盒Library目錄

@return Library目錄

*/

+ (NSString *)getLibraryDirectory;

/**

獲取沙盒Library/Caches目錄

@return Library/Caches目錄

*/

+ (NSString *)getCachesDirectory;

/**

獲取沙盒Preference目錄

@return Preference目錄

*/

+ (NSString *)getPreferenceDirectory;

/**

獲取沙盒Tmp目錄

@return Tmp目錄

*/

+ (NSString *)getTmpDirectory;

#pragma mark - 清除沙盒目錄文件內容 -

/**

根據路徑返回目錄或文件的大小

@param path 文件目錄

@return 目錄文件大小

*/

+ (CGFloat)sizeWithFilePath:(NSString *)path;

/**

獲得指定目錄下的全部文件

@param dirPath 指定目錄

@return 全部文件

*/

+ (NSArray *)getAllFileNames:(NSString *)dirPath;

/**

刪除指定目錄或文件

@param path 指定目錄或文件

@return 刪除結果

*/

+ (BOOL)clearCachesWithFilePath:(NSString *)path;

/**

清空指定目錄下文件

@param dirPath 指定目錄

@return 清除結果

*/

+ (BOOL)clearCachesFromDirectoryPath:(NSString *)dirPath;

/**

清理圖片緩存

@return 圖片緩存

*/

+ (void)clearCachesImage;

/**

清理網頁緩存

@return 網頁緩存

*/

+ (BOOL)clearCachesWeb;

/**

清理信息類

@return 信息類緩存

*/

+ (BOOL)clearCachesInfo;

/** 清理全部緩存 */

+ (void)clearAllCaches;

/**

得到緩存大小

@return 緩存大小

*/

+ (NSUInteger)getCachesSize;

/**

獲取緩存大小字符串

@return 緩存大小字符串

*/

+ (NSString *)getCachesSizeString;

/** 建立cache/User文件夾 */

+ (void)createUserCacheFile;

/** 獲取cache/User文件夾路徑 */

+ (NSString *)getCacheUserPath;

#pragma mark - 緩存歸檔與解檔 -

/** 歸檔羣組列表 */

+ (void)archiveGroupList:(NSMutableArray *)groupArr;

/** 歸檔活動列表 */

+ (void)archiveActivityList:(NSMutableArray *)actArr;

/** 載入羣組列表緩存 */

+ (NSMutableArray *)unarchiveGroupList;

/** 載入活動列表緩存 */

+ (NSMutableArray *)unachiveActivityList;



#import "LZSandBoxManager.h"

#import "SDImageCache.h"

#define FILE_CACHE_USER                @"User"

#define FILE_CACHE_WebKit              @"WebKit"

#define FILE_Group_list                @"GroupList.plist"      // 羣組列表

#define FILE_Activity_list             @"ActivityList.plist"    // 羣組列表

@implementation LZSandBoxManager

+ (NSFileManager *)initFileManager {

NSFileManager *manager;

if (manager == nil) {

manager = [NSFileManager defaultManager];

}

return manager;

}

#pragma mark - 獲取沙盒目錄 -

/** 獲取沙盒Document目錄 */

+ (NSString *)getDocumentDirectory {

return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

}

/** 獲取沙盒Liabrary目錄 */

+ (NSString *)getLibraryDirectory {

return NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];

}

/** 獲取沙盒Library/Caches目錄 */

+ (NSString *)getCachesDirectory {

return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];

}

/** 獲取沙盒Preference目錄 */

+ (NSString *)getPreferenceDirectory {

return NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES)[0];

}

/** 獲取沙盒Tmp目錄 */

+ (NSString *)getTmpDirectory {

return NSTemporaryDirectory();

}

#pragma mark - 清除沙盒目錄文件內容 -

/** 根據路徑返回目錄或文件的大小 */

+ (CGFloat)sizeWithFilePath:(NSString *)path {

// 1.得到文件管理權限

NSFileManager *manager = [self initFileManager];

// 2.檢測路徑合理性

BOOL directory = NO;

BOOL exist = [manager fileExistsAtPath:path isDirectory:&directory];

if (!exist) return 0;

// 3.判斷是否爲文件夾

// 文件夾

if (directory) {

// 這個方法能得到這個文件夾下面的全部子路徑(直接\間接子路徑)

NSArray *subPaths = [manager subpathsAtPath:path];

int totalSize = 0;

for (NSString *subPath in subPaths) {

NSString *fullSubPath = [path stringByAppendingPathComponent:subPath]; // 拼出子目錄的全路徑

BOOL directory = NO;

[manager fileExistsAtPath:fullSubPath isDirectory:&directory];

// 子路徑是個文件

if (!directory) {

NSDictionary *attrs = [manager attributesOfItemAtPath:fullSubPath error:nil];

totalSize += [attrs[NSFileSize] intValue];

}

}

return totalSize / (1024*1024.0);

}

// 文件

else  {

NSDictionary *attrs = [manager attributesOfItemAtPath:path error:nil];

return [attrs[NSFileSize] intValue] / (1024*1024.0);

}

}

/** 獲得指定目錄下的全部文件 */

+ (NSArray *)getAllFileNames:(NSString *)dirPath {

NSArray *files = [[self initFileManager] subpathsOfDirectoryAtPath:dirPath error:nil];

return files;

}

/** 刪除指定目錄或文件 */

+ (BOOL)clearCachesWithFilePath:(NSString *)path {

return [[self initFileManager] removeItemAtPath:path error:nil];

}

/** 清空指定目錄下文件 */

+ (BOOL)clearCachesFromDirectoryPath:(NSString *)dirPath {

// 得到所有文件數組

NSArray *fileArr = [self getAllFileNames:dirPath];

BOOL flag = NO;

for (NSString *fileName in fileArr) {

NSString *filePath = [dirPath stringByAppendingPathComponent:fileName];

flag = [self clearCachesWithFilePath:filePath];

if (!flag) {

break;

}

}

return flag;

}

/** 清理圖片緩存 */

+ (void)clearCachesImage {

SDImageCache *sdCache = [SDImageCache sharedImageCache];

[sdCache clearDisk];

}

/** 清理網頁緩存 */

+ (BOOL)clearCachesWeb {

NSString *path = [[self getCachesDirectory] stringByAppendingPathComponent:FILE_CACHE_WebKit];

return [self clearCachesWithFilePath:path];

}

/** 清理信息類緩存 */

+ (BOOL)clearCachesInfo {

return [self clearCachesWithFilePath:[self getCacheUserPath]];

}

/** 清理全部緩存 */

+ (void)clearAllCaches {

[self clearCachesImage];

[self clearCachesWeb];

[self clearCachesInfo];

}

/** 獲取緩存大小 */

+ (NSUInteger)getCachesSize {

NSUInteger totalSize = 0;

// 1.動態草稿

// 2.SDWebImage緩存大小

SDImageCache *sdCache = [SDImageCache sharedImageCache];

NSUInteger sdCacheSize = [sdCache getSize];

// 3.用戶瀏覽信息列表緩存

NSArray *filesArr = [self getAllFileNames:[self getCacheUserPath]];

NSUInteger infoSize = 0;

for (NSString *filePath in filesArr) {

NSString *filePathAppend = [[self getCacheUserPath] stringByAppendingPathComponent:filePath];

NSData *data = [NSData dataWithContentsOfFile:filePathAppend];

infoSize += data.length;

}

// 4.WebKit緩存

NSString *webKitPath = [[self getCachesDirectory] stringByAppendingPathComponent:FILE_CACHE_WebKit];

NSArray *webFileArr = [self getAllFileNames:webKitPath];

NSUInteger webSize = 0;

for (NSString *filePath in webFileArr) {

NSString *filePathAppend = [webKitPath stringByAppendingPathComponent:filePath];

NSData *data = [NSData dataWithContentsOfFile:filePathAppend];

webSize += data.length;

}

totalSize = sdCacheSize + infoSize + webSize;

return totalSize;

}

/** 獲取緩存大小字符串 */

+ (NSString *)getCachesSizeString {

NSUInteger cacheSize =  [self getCachesSize] / 1024 / 1024;

if (cacheSize == 0) return nil;

NSString *cacheSizeStr = cacheSize >= 1 ? [NSString stringWithFormat:@"%luM", (unsigned long)cacheSize] : [NSString stringWithFormat:@"%luK", (unsigned long)cacheSize];

return cacheSizeStr;

}

/** 建立cache/User文件夾 */

+ (void)createUserCacheFile {

NSFileManager *fm = [self initFileManager];

NSString *path = [[self getCachesDirectory] stringByAppendingPathComponent:FILE_CACHE_USER];

if (![fm fileExistsAtPath:path]) {

[fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];

} else

NSLog(@"File path Cache/User has been existed !");

}

/** 獲取cache/User文件夾路徑 */

+ (NSString *)getCacheUserPath {

NSString *userPath = [[self getCachesDirectory] stringByAppendingPathComponent:FILE_CACHE_USER];

return userPath;

}

#pragma mark - 緩存歸檔與解檔 -

/** 歸檔羣組列表 */

+ (void)archiveGroupList:(NSMutableArray *)groupArr {

[self createUserCacheFile];

NSString *path = [[LZSandBoxManager getCacheUserPath] stringByAppendingPathComponent:FILE_Group_list];

[NSKeyedArchiver archiveRootObject:groupArr toFile:path];

}

/** 歸檔活動列表 */

+ (void)archiveActivityList:(NSMutableArray *)actArr {

[self createUserCacheFile];

NSString *path = [[LZSandBoxManager getCacheUserPath] stringByAppendingPathComponent:FILE_Activity_list];

[NSKeyedArchiver archiveRootObject:actArr toFile:path];

}

/** 載入羣組列表緩存 */

+ (NSMutableArray *)unarchiveGroupList {

NSString *path = [[LZSandBoxManager getCacheUserPath] stringByAppendingPathComponent:FILE_Group_list];

NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

return array;

}

/** 載入活動列表緩存 */

+ (NSMutableArray *)unachiveActivityList {

NSString *path = [[LZSandBoxManager getCacheUserPath] stringByAppendingPathComponent:FILE_Activity_list];

NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

return array;

}
相關文章
相關標籤/搜索