http://www.jianshu.com/p/fe00883ad3d2
https://github.com/suifengqjn/VideoRecord
此工程下的,XCFileManager.h文件
#import
@interface XCFileManager : NSObject
#pragma mark - 沙盒目录相关
// 沙盒的主目录路径
+ (NSString *)homeDir;
// 沙盒中Documents的目录路径
+ (NSString *)documentsDir;
// 沙盒中Library的目录路径
+ (NSString *)libraryDir;
// 沙盒中Libarary/Preferences的目录路径
+ (NSString *)preferencesDir;
// 沙盒中Library/Caches的目录路径
+ (NSString *)cachesDir;
// 沙盒中tmp的目录路径
+ (NSString *)tmpDir;
#pragma mark - 遍历文件夹
/**
文件遍历
@param path 目录的绝对路径
@param deep 是否深遍历 (1. 浅遍历:返回当前目录下的所有文件和文件夹;
2. 深遍历:返回当前目录下及子目录下的所有文件和文件夹)
@return 遍历结果数组
*/
+ (NSArray *)listFilesInDirectoryAtPath:(NSString *)path deep:(BOOL)deep;
// 遍历沙盒主目录
+ (NSArray *)listFilesInHomeDirectoryByDeep:(BOOL)deep;
// 遍历Documents目录
+ (NSArray *)listFilesInDocumentDirectoryByDeep:(BOOL)deep;
// 遍历Library目录
+ (NSArray *)listFilesInLibraryDirectoryByDeep:(BOOL)deep;
// 遍历Caches目录
+ (NSArray *)listFilesInCachesDirectoryByDeep:(BOOL)deep;
// 遍历tmp目录
+ (NSArray *)listFilesInTmpDirectoryByDeep:(BOOL)deep;
#pragma mark - 获取文件属性
// 根据key获取文件某个属性
+ (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key;
// 根据key获取文件某个属性(错误信息error)
+ (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key error:(NSError **)error;
// 获取文件属性集合
+ (NSDictionary *)attributesOfItemAtPath:(NSString *)path;
// 获取文件属性集合(错误信息error)
+ (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error;
#pragma mark - 创建文件(夹)
// 创建文件夹
+ (BOOL)createDirectoryAtPath:(NSString *)path;
// 创建文件夹(错误信息error)
+ (BOOL)createDirectoryAtPath:(NSString *)path error:(NSError **)error;
// 创建文件
+ (BOOL)createFileAtPath:(NSString *)path;
// 创建文件(错误信息error)
+ (BOOL)createFileAtPath:(NSString *)path error:(NSError **)error;
// 创建文件,是否覆盖
+ (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite;
// 创建文件,是否覆盖(错误信息error)
+ (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite error:(NSError **)error;
// 创建文件,文件内容
+ (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content;
// 创建文件,文件内容(错误信息error)
+ (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError **)error;
// 创建文件,文件内容,是否覆盖
+ (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite;
// 创建文件,文件内容,是否覆盖(错误信息error)
+ (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite error:(NSError **)error;
// 获取创建文件时间
+ (NSDate *)creationDateOfItemAtPath:(NSString *)path;
// 获取创建文件时间(错误信息error)
+ (NSDate *)creationDateOfItemAtPath:(NSString *)path error:(NSError **)error;
// 获取文件修改时间
+ (NSDate *)modificationDateOfItemAtPath:(NSString *)path;
// 获取文件修改时间(错误信息error)
+ (NSDate *)modificationDateOfItemAtPath:(NSString *)path error:(NSError **)error;
#pragma mark - 删除文件(夹)
// 删除文件
+ (BOOL)removeItemAtPath:(NSString *)path;
// 删除文件(错误信息error)
+ (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error;
// 清空Caches文件夹
+ (BOOL)clearCachesDirectory;
// 清空tmp文件夹
+ (BOOL)clearTmpDirectory;
#pragma mark - 复制文件(夹)
// 复制文件
+ (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath;
// 复制文件(错误信息error)
+ (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError **)error;
// 复制文件,是否覆盖
+ (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite;
// 复制文件,是否覆盖(错误信息error)
+ (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError **)error;
#pragma mark - 移动文件(夹)
// 移动文件
+ (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath;
// 移动文件(错误信息error)
+ (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError **)error;
// 移动文件,是否覆盖
+ (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite;
// 移动文件,是否覆盖(错误信息error)
+ (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError **)error;
#pragma mark - 根据URL获取文件名
// 根据文件路径获取文件名称,是否需要后缀
+ (NSString *)fileNameAtPath:(NSString *)path suffix:(BOOL)suffix;
// 获取文件所在的文件夹路径
+ (NSString *)directoryAtPath:(NSString *)path;
// 根据文件路径获取文件扩展类型
+ (NSString *)suffixAtPath:(NSString *)path;
#pragma mark - 判断文件(夹)是否存在
// 判断文件路径是否存在
+ (BOOL)isExistsAtPath:(NSString *)path;
// 判断路径是否为空(判空条件是文件大小为0,或者是文件夹下没有子文件)
+ (BOOL)isEmptyItemAtPath:(NSString *)path;
// 判断路径是否为空(错误信息error)
+ (BOOL)isEmptyItemAtPath:(NSString *)path error:(NSError **)error;
// 判断目录是否是文件夹
+ (BOOL)isDirectoryAtPath:(NSString *)path;
// 判断目录是否是文件夹(错误信息error)
+ (BOOL)isDirectoryAtPath:(NSString *)path error:(NSError **)error;
// 判断目录是否是文件
+ (BOOL)isFileAtPath:(NSString *)path;
// 判断目录是否是文件(错误信息error)
+ (BOOL)isFileAtPath:(NSString *)path error:(NSError **)error;
// 判断目录是否可以执行
+ (BOOL)isExecutableItemAtPath:(NSString *)path;
// 判断目录是否可读
+ (BOOL)isReadableItemAtPath:(NSString *)path;
// 判断目录是否可写
+ (BOOL)isWritableItemAtPath:(NSString *)path;
#pragma mark - 获取文件(夹)大小
// 获取目录大小
+ (NSNumber *)sizeOfItemAtPath:(NSString *)path;
// 获取目录大小(错误信息error)
+ (NSNumber *)sizeOfItemAtPath:(NSString *)path error:(NSError **)error;
// 获取文件大小
+ (NSNumber *)sizeOfFileAtPath:(NSString *)path;
// 获取文件大小(错误信息error)
+ (NSNumber *)sizeOfFileAtPath:(NSString *)path error:(NSError **)error;
// 获取文件夹大小
+ (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path;
// 获取文件夹大小(错误信息error)
+ (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path error:(NSError **)error;
// 获取目录大小,返回格式化后的数值
+ (NSString *)sizeFormattedOfItemAtPath:(NSString *)path;
// 获取目录大小,返回格式化后的数值(错误信息error)
+ (NSString *)sizeFormattedOfItemAtPath:(NSString *)path error:(NSError **)error;
// 获取文件大小,返回格式化后的数值
+ (NSString *)sizeFormattedOfFileAtPath:(NSString *)path;
// 获取文件大小,返回格式化后的数值(错误信息error)
+ (NSString *)sizeFormattedOfFileAtPath:(NSString *)path error:(NSError **)error;
// 获取文件夹大小,返回格式化后的数值
+ (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path;
// 获取文件夹大小,返回格式化后的数值(错误信息error)
+ (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path error:(NSError **)error;
#pragma mark - 写入文件内容
// 写入文件内容
+ (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content;
// 写入文件内容(错误信息error)
+ (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError **)error;
@end
#import "XCFileManager.h"
@interface XCFileManager ()
@property (strong, nonatomic) NSFileManager *manager;
@end
@implementation XCFileManager
#pragma mark - 沙盒目录相关
+ (NSString *)homeDir {
return NSHomeDirectory();
}
+ (NSString *)documentsDir {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
}
+ (NSString *)libraryDir {
return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];;
}
+ (NSString *)preferencesDir {
NSString *libraryDir = [self libraryDir];
return [libraryDir stringByAppendingPathComponent:@"Preferences"];
}
+ (NSString *)cachesDir {
return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
}
+ (NSString *)tmpDir {
return NSTemporaryDirectory();
}
#pragma mark - 遍历文件夹
+ (NSArray *)listFilesInDirectoryAtPath:(NSString *)path deep:(BOOL)deep {
NSArray *listArr;
NSError *error;
NSFileManager *manager = [NSFileManager defaultManager];
if (deep) {
// 深遍历
NSArray *deepArr = [manager subpathsOfDirectoryAtPath:path error:&error];
if (!error) {
listArr = deepArr;
}else {
listArr = nil;
}
}else {
// 浅遍历
NSArray *shallowArr = [manager contentsOfDirectoryAtPath:path error:&error];
if (!error) {
listArr = shallowArr;
}else {
listArr = nil;
}
}
return listArr;
}
+ (NSArray *)listFilesInHomeDirectoryByDeep:(BOOL)deep {
return [self listFilesInDirectoryAtPath:[self homeDir] deep:deep];
}
+ (NSArray *)listFilesInLibraryDirectoryByDeep:(BOOL)deep {
return [self listFilesInDirectoryAtPath:[self libraryDir] deep:deep];
}
+ (NSArray *)listFilesInDocumentDirectoryByDeep:(BOOL)deep {
return [self listFilesInDirectoryAtPath:[self documentsDir] deep:deep];
}
+ (NSArray *)listFilesInTmpDirectoryByDeep:(BOOL)deep {
return [self listFilesInDirectoryAtPath:[self tmpDir] deep:deep];
}
+ (NSArray *)listFilesInCachesDirectoryByDeep:(BOOL)deep {
return [self listFilesInDirectoryAtPath:[self cachesDir] deep:deep];
}
#pragma mark - 获取文件属性
+ (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key {
return [[self attributesOfItemAtPath:path] objectForKey:key];
}
+ (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key error:(NSError *__autoreleasing *)error {
return [[self attributesOfItemAtPath:path error:error] objectForKey:key];
}
+ (NSDictionary *)attributesOfItemAtPath:(NSString *)path {
return [self attributesOfItemAtPath:path error:nil];
}
+ (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
return [[NSFileManager defaultManager] attributesOfItemAtPath:path error:error];
}
#pragma mark - 创建文件(夹)
+ (BOOL)createDirectoryAtPath:(NSString *)path {
return [self createDirectoryAtPath:path error:nil];
}
+ (BOOL)createDirectoryAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
NSFileManager *manager = [NSFileManager defaultManager];
BOOL isSuccess = [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:error];
return isSuccess;
}
+ (BOOL)createFileAtPath:(NSString *)path {
return [self createFileAtPath:path content:nil overwrite:YES error:nil];
}
+ (BOOL)createFileAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
return [self createFileAtPath:path content:nil overwrite:YES error:error];
}
+ (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite {
return [self createFileAtPath:path content:nil overwrite:overwrite error:nil];
}
+ (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite error:(NSError *__autoreleasing *)error {
return [self createFileAtPath:path content:nil overwrite:overwrite error:error];
}
+ (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content {
return [self createFileAtPath:path content:content overwrite:YES error:nil];
}
+ (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError *__autoreleasing *)error {
return [self createFileAtPath:path content:content overwrite:YES error:error];
}
+ (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite {
return [self createFileAtPath:path content:content overwrite:overwrite error:nil];
}
+ (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite error:(NSError *__autoreleasing *)error {
// 如果文件夹路径不存在,那么先创建文件夹
NSString *directoryPath = [self directoryAtPath:path];
if (![self isExistsAtPath:directoryPath]) {
// 创建文件夹
if (![self createDirectoryAtPath:directoryPath error:error]) {
return NO;
}
}
// 如果文件存在,并不想覆盖,那么直接返回YES。
if (!overwrite) {
if ([self isExistsAtPath:path]) {
return YES;
}
}
// 创建文件
BOOL isSuccess = [[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
if (content) {
[self writeFileAtPath:path content:content error:error];
}
return isSuccess;
}
+ (NSDate *)creationDateOfItemAtPath:(NSString *)path {
return [self creationDateOfItemAtPath:path error:nil];
}
+ (NSDate *)creationDateOfItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
return (NSDate *)[self attributeOfItemAtPath:path forKey:NSFileCreationDate error:error];
}
+ (NSDate *)modificationDateOfItemAtPath:(NSString *)path {
return [self modificationDateOfItemAtPath:path error:nil];
}
+ (NSDate *)modificationDateOfItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
return (NSDate *)[self attributeOfItemAtPath:path forKey:NSFileModificationDate error:error];
}
#pragma mark - 删除文件(夹)
+ (BOOL)removeItemAtPath:(NSString *)path {
return [self removeItemAtPath:path error:nil];
}
+ (BOOL)removeItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
return [[NSFileManager defaultManager] removeItemAtPath:path error:error];
}
+ (BOOL)clearCachesDirectory {
NSArray *subFiles = [self listFilesInCachesDirectoryByDeep:NO];
BOOL isSuccess = YES;
for (NSString *file in subFiles) {
NSString *absolutePath = [[self cachesDir] stringByAppendingPathComponent:file];
isSuccess &= [self removeItemAtPath:absolutePath];
}
return isSuccess;
}
+ (BOOL)clearTmpDirectory {
NSArray *subFiles = [self listFilesInTmpDirectoryByDeep:NO];
BOOL isSuccess = YES;
for (NSString *file in subFiles) {
NSString *absolutePath = [[self tmpDir] stringByAppendingPathComponent:file];
isSuccess &= [self removeItemAtPath:absolutePath];
}
return isSuccess;
}
#pragma mark - 复制文件(夹)
+ (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath {
return [self copyItemAtPath:path toPath:toPath overwrite:NO error:nil];
}
+ (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError *__autoreleasing *)error {
return [self copyItemAtPath:path toPath:toPath overwrite:NO error:error];
}
+ (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite {
return [self copyItemAtPath:path toPath:toPath overwrite:overwrite error:nil];
}
+ (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError *__autoreleasing *)error {
// 先要保证源文件路径存在,不然抛出异常
if (![self isExistsAtPath:path]) {
[NSException raise:@"非法的源文件路径" format:@"源文件路径%@不存在,请检查源文件路径", path];
return NO;
}
NSString *toDirPath = [self directoryAtPath:toPath];
if (![self isExistsAtPath:toDirPath]) {
// 创建复制路径
if (![self createDirectoryAtPath:toDirPath error:error]) {
return NO;
}
}
// 如果覆盖,那么先删掉原文件
if (overwrite) {
if ([self isExistsAtPath:toPath]) {
[self removeItemAtPath:toPath error:error];
}
}
// 复制文件
BOOL isSuccess = [[NSFileManager defaultManager] copyItemAtPath:path toPath:toPath error:error];
return isSuccess;
}
#pragma mark - 移动文件(夹)
+ (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath {
return [self moveItemAtPath:path toPath:toPath overwrite:NO error:nil];
}
+ (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError *__autoreleasing *)error {
return [self moveItemAtPath:path toPath:toPath overwrite:NO error:error];
}
+ (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite {
return [self moveItemAtPath:path toPath:toPath overwrite:overwrite error:nil];
}
+ (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError *__autoreleasing *)error {
// 先要保证源文件路径存在,不然抛出异常
if (![self isExistsAtPath:path]) {
[NSException raise:@"非法的源文件路径" format:@"源文件路径%@不存在,请检查源文件路径", path];
return NO;
}
NSString *toDirPath = [self directoryAtPath:toPath];
if (![self isExistsAtPath:toDirPath]) {
// 创建移动路径
if (![self createDirectoryAtPath:toDirPath error:error]) {
return NO;
}
}
// 如果覆盖,那么先删掉原文件
if ([self isExistsAtPath:toPath]) {
if (overwrite) {
[self removeItemAtPath:toPath error:error];
}else {
[self removeItemAtPath:path error:error];
return YES;
}
}
// 移动文件
BOOL isSuccess = [[NSFileManager defaultManager] moveItemAtPath:path toPath:toPath error:error];
return isSuccess;
}
#pragma mark - 根据URL获取文件名
+ (NSString *)fileNameAtPath:(NSString *)path suffix:(BOOL)suffix {
NSString *fileName = [path lastPathComponent];
if (!suffix) {
fileName = [fileName stringByDeletingPathExtension];
}
return fileName;
}
+ (NSString *)directoryAtPath:(NSString *)path {
return [path stringByDeletingLastPathComponent];
}
+ (NSString *)suffixAtPath:(NSString *)path {
return [path pathExtension];
}
#pragma mark - 判断文件(夹)是否存在
+ (BOOL)isExistsAtPath:(NSString *)path {
return [[NSFileManager defaultManager] fileExistsAtPath:path];
}
+ (BOOL)isEmptyItemAtPath:(NSString *)path {
return [self isEmptyItemAtPath:path error:nil];
}
+ (BOOL)isEmptyItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
return ([self isFileAtPath:path error:error] &&
[[self sizeOfItemAtPath:path error:error] intValue] == 0) ||
([self isDirectoryAtPath:path error:error] &&
[[self listFilesInDirectoryAtPath:path deep:NO] count] == 0);
}
+ (BOOL)isDirectoryAtPath:(NSString *)path {
return [self isDirectoryAtPath:path error:nil];
}
+ (BOOL)isDirectoryAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
return ([self attributeOfItemAtPath:path forKey:NSFileType error:error] == NSFileTypeDirectory);
}
+ (BOOL)isFileAtPath:(NSString *)path {
return [self isFileAtPath:path error:nil];
}
+ (BOOL)isFileAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
return ([self attributeOfItemAtPath:path forKey:NSFileType error:error] == NSFileTypeRegular);
}
+ (BOOL)isExecutableItemAtPath:(NSString *)path {
return [[NSFileManager defaultManager] isExecutableFileAtPath:path];
}
+ (BOOL)isReadableItemAtPath:(NSString *)path {
return [[NSFileManager defaultManager] isReadableFileAtPath:path];
}
+ (BOOL)isWritableItemAtPath:(NSString *)path {
return [[NSFileManager defaultManager] isWritableFileAtPath:path];
}
#pragma mark - 获取文件(夹)大小
+ (NSNumber *)sizeOfItemAtPath:(NSString *)path {
return [self sizeOfItemAtPath:path error:nil];
}
+ (NSNumber *)sizeOfItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
return (NSNumber *)[self attributeOfItemAtPath:path forKey:NSFileSize error:error];
}
+ (NSNumber *)sizeOfFileAtPath:(NSString *)path {
return [self sizeOfFileAtPath:path error:nil];
}
+ (NSNumber *)sizeOfFileAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
if ([self isFileAtPath:path error:error]) {
return [self sizeOfItemAtPath:path error:error];
}
return nil;
}
+ (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path {
return [self sizeOfDirectoryAtPath:path error:nil];
}
+ (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
if ([self isDirectoryAtPath:path error:error]) {
NSArray *subPaths = [self listFilesInDirectoryAtPath:path deep:YES];
NSEnumerator *contentsEnumurator = [subPaths objectEnumerator];
NSString *file;
unsigned long long int folderSize = 0;
while (file = [contentsEnumurator nextObject]) {
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[path stringByAppendingPathComponent:file] error:nil];
folderSize += [[fileAttributes objectForKey:NSFileSize] intValue];
}
return [NSNumber numberWithUnsignedLongLong:folderSize];
}
return nil;
}
+ (NSString *)sizeFormattedOfItemAtPath:(NSString *)path {
return [self sizeFormattedOfItemAtPath:path error:nil];
}
+ (NSString *)sizeFormattedOfItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
NSNumber *size = [self sizeOfItemAtPath:path error:error];
if (size) {
return [self sizeFormatted:size];
}
return nil;
}
+ (NSString *)sizeFormattedOfFileAtPath:(NSString *)path {
return [self sizeFormattedOfFileAtPath:path error:nil];
}
+ (NSString *)sizeFormattedOfFileAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
NSNumber *size = [self sizeOfFileAtPath:path error:error];
if (size) {
return [self sizeFormatted:size];
}
return nil;
}
+ (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path {
return [self sizeFormattedOfDirectoryAtPath:path error:nil];
}
+ (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path error:(NSError *__autoreleasing *)error {
NSNumber *size = [self sizeOfDirectoryAtPath:path error:error];
if (size) {
return [self sizeFormatted:size];
}
return nil;
}
#pragma mark - 写入文件内容
+ (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content {
return [self writeFileAtPath:path content:content error:nil];
}
+ (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError *__autoreleasing *)error {
if (!content) {
[NSException raise:@"非法的文件内容" format:@"文件内容不能为nil"];
return NO;
}
if ([self isExistsAtPath:path]) {
if ([content isKindOfClass:[NSMutableArray class]]) {
[(NSMutableArray *)content writeToFile:path atomically:YES];
}else if ([content isKindOfClass:[NSArray class]]) {
[(NSArray *)content writeToFile:path atomically:YES];
}else if ([content isKindOfClass:[NSMutableData class]]) {
[(NSMutableData *)content writeToFile:path atomically:YES];
}else if ([content isKindOfClass:[NSData class]]) {
[(NSData *)content writeToFile:path atomically:YES];
}else if ([content isKindOfClass:[NSMutableDictionary class]]) {
[(NSMutableDictionary *)content writeToFile:path atomically:YES];
}else if ([content isKindOfClass:[NSDictionary class]]) {
[(NSDictionary *)content writeToFile:path atomically:YES];
}else if ([content isKindOfClass:[NSJSONSerialization class]]) {
[(NSDictionary *)content writeToFile:path atomically:YES];
}else if ([content isKindOfClass:[NSMutableString class]]) {
[[((NSString *)content) dataUsingEncoding:NSUTF8StringEncoding] writeToFile:path atomically:YES];
}else if ([content isKindOfClass:[NSString class]]) {
[[((NSString *)content) dataUsingEncoding:NSUTF8StringEncoding] writeToFile:path atomically:YES];
}else if ([content isKindOfClass:[UIImage class]]) {
[UIImagePNGRepresentation((UIImage *)content) writeToFile:path atomically:YES];
}else if ([content conformsToProtocol:@protocol(NSCoding)]) {
[NSKeyedArchiver archiveRootObject:content toFile:path];
}else {
[NSException raise:@"非法的文件内容" format:@"文件类型%@异常,无法被处理。", NSStringFromClass([content class])];
return NO;
}
}else {
return NO;
}
return YES;
}
#pragma mark - private methods
+ (BOOL)isNotError:(NSError **)error {
return ((error == nil) || ((*error) == nil));
}
+(NSString *)sizeFormatted:(NSNumber *)size {
return [NSByteCountFormatter stringFromByteCount:[size unsignedLongLongValue] countStyle:NSByteCountFormatterCountStyleFile];
}
@end