iOS - 拷貝resource資原始檔夾

weixin_34308389發表於2017-06-11

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

相關文章