#pragma mark -- 拷貝資源
-(void)zip{ NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; //Document目錄 NSString *dataPath=[NSString stringWithFormat:@"%@/Data/Raw",[[NSBundle mainBundle] resourcePath]]; // 項目包中的路徑 NSFileManager* fileManager = [NSFileManager defaultManager]; // 先刪除ClientRes文件夾 NSString * documentDir = [docPath stringByAppendingPathComponent:@"/ClientRes"]; BOOL isDir = NO; BOOL existed = [fileManager fileExistsAtPath:documentDir isDirectory:&isDir];
if ( !(isDir == YES && existed == YES) ) { KkLog(@"ClientRes不存在"); }else { BOOL s=[[NSFileManager defaultManager] removeItemAtPath:documentDir error:nil]; if (s) { KkLog(@"刪除成功"); }else{ KkLog(@"刪除失敗"); } }
//下面是對該文件進行制定路徑的保存
[fileManager createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:nil]; //取得一個目錄下得所有文件名 NSArray *filels = [fileManager subpathsAtPath:dataPath ];
for(int i = 0;i < filels.count;i++) { BOOL isdir = NO; NSString * curpath = [filels objectAtIndex:i]; NSString * npath = [NSString stringWithFormat:@"%@/%@",dataPath,curpath]; NSString *copyPath = [NSString stringWithFormat:@"%@/%@",docPath,curpath];
KkLog(@"i ---- %d curpath ---- %@ npath ---- %@",i,curpath,npath);
[fileManager fileExistsAtPath:npath isDirectory:&isdir];
if (isdir && YES)
{
[self createFolder:copyPath];
}
else
{
if(![self copyMissingFile:npath toPath:copyPath]){
[fileManager copyItemAtPath:npath toPath:copyPath error:nil];
}
}
}
}`
/**
- @brief 創(chuàng)建文件夾
- @param createDir 創(chuàng)建文件夾路徑
*/
- (void)createFolder:(NSString *)createDir { BOOL isDir = NO; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL existed = [fileManager fileExistsAtPath:createDir isDirectory:&isDir]; if ( !(isDir == YES && existed == YES) ) { [fileManager createDirectoryAtPath:createDir withIntermediateDirectories:YES attributes:nil error:nil]; }else { NSLog(@"FileDir is exists."); } }
/**
- @brief 把Resource文件夾下的save1.dat拷貝到沙盒
- @param sourcePath Resource文件路徑
- @param toPath 把文件拷貝到XXX文件夾
- @return BOOL
*/
- (BOOL)copyMissingFile:(NSString *)sourcePath toPath:(NSString *)toPath { [self deletDocumentsFile:toPath]; BOOL retVal = YES; // If the file already exists, we'll return success… NSString * finalLocation = [toPath stringByAppendingPathComponent:[sourcePath lastPathComponent]]; if (![[NSFileManager defaultManager] fileExistsAtPath:finalLocation]) { retVal = [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:finalLocation error:NULL]; } return retVal; }