// // CJFileUtility.m // // Created by chenjian on 4/25/12. // Copyright (c) 2012 cj. All rights reserved. // #import "CJFileUtility.h" #import @implementation CJFileUtility + (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *)path { NSURL *url = [NSURL fileURLWithPath:path]; return [url setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil]; } + (BOOL)fileExists:(NSString *)fileName { return [[NSFileManager defaultManager] fileExistsAtPath:fileName]; } + (long long) fileSizeAtPath:(NSString*)filePath { NSFileManager* manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:filePath]){ return [[manager attributesOfItemAtPath:filePath error:nil] fileSize]; } return 0; } + (BOOL)writeToFile:(NSString *)fileName data:(NSData*)data { return [data writeToFile:fileName atomically:YES]; } +(BOOL)appendToFile:(NSString *)fileName data:(NSData*)data { if ([CJFileUtility fileExists:fileName]) { // 取得修改的文件句柄 NSFileHandle *fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:fileName]; // 移动指针到文件末尾 [fileHandler seekToEndOfFile]; // 追加内容 [fileHandler writeData:data]; return YES; } else { return [data writeToFile:fileName atomically:YES]; } } +(BOOL)deleteFile:(NSString *)fileName { return [[NSFileManager defaultManager] removeItemAtPath:fileName error:nil]; } +(BOOL)renameFile:(NSString *)oldName newName:(NSString*)newName { if ([CJFileUtility fileExists:oldName]) { [CJFileUtility deleteFile:newName]; return [[NSFileManager defaultManager] moveItemAtPath:oldName toPath:newName error:nil]; } return NO; } + (BOOL)copyFile:(NSString *)srcFile to:(NSString *)dstFile { if ([CJFileUtility fileExists:srcFile]) { return [[NSFileManager defaultManager] copyItemAtPath:srcFile toPath:dstFile error:nil]; } return NO; } + (BOOL)moveFile:(NSString *)srcFile to:(NSString *)dstFile { if ([CJFileUtility fileExists:srcFile]) { return [[NSFileManager defaultManager] moveItemAtPath:srcFile toPath:dstFile error:nil]; } return NO; } + (BOOL)createPath:(NSString *)path { return [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; } +(NSString *)documentsPath { NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); return [paths objectAtIndex:0]; } + (NSString *)cachePath { NSArray *paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES); return [paths objectAtIndex:0]; } + (NSString *)cachePathWithFile:(NSString *)fileName { return [[CJFileUtility cachePath] stringByAppendingPathComponent:fileName]; } +(NSString *)tempPath { return [NSString stringWithFormat:@"%@/%@", NSHomeDirectory(), @"tmp"]; } + (NSString *)documentsPathSubDir:(NSString*)subDir { NSString* path = [[CJFileUtility documentsPath] stringByAppendingPathComponent:subDir]; if (![[NSFileManager defaultManager] fileExistsAtPath:path]) { [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; } return path; } + (NSString *)documentsPathSubDir:(NSString*)subDir andFile:(NSString*)fileName { NSString* path = [[[[CJFileUtility documentsPath] stringByAppendingPathComponent:subDir] stringByAppendingPathComponent:fileName] stringByDeletingLastPathComponent]; if (![[NSFileManager defaultManager] fileExistsAtPath:path]) { [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; } return [path stringByAppendingPathComponent:[fileName lastPathComponent]]; } + (NSString *)documentsPathSubDir:(NSString*)subDir andUrl:(NSString*)url { NSString * fileName = [url lastPathComponent]; return [CJFileUtility documentsPathSubDir:subDir andFile:fileName]; } + (NSString *)changeFileExt:(NSString *)fileName ext:(NSString *)ext { NSString *noExt = [fileName stringByDeletingPathExtension]; return [noExt stringByAppendingPathExtension:ext]; } + (NSString *)changeFilePath:(NSString *)path fileName:(NSString *)fileName { NSString *filePath = [path stringByDeletingLastPathComponent]; return [filePath stringByAppendingPathComponent:fileName]; } @end