iOS - 拷贝resource资源文件夹

#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 创建文件夹
  • @param createDir 创建文件夹路径
    */

- (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; }

你可能感兴趣的:(iOS - 拷贝resource资源文件夹)