-(void)zip{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docspath = [paths objectAtIndex:0];
NSString *eawPath = [[NSBundle mainBundle] pathForResource:@"ClientRes" ofType:@"zip"];
NSLog(@"eawPath ===%@ ",eawPath);
NSString *eawRPath = [docspath stringByAppendingPathComponent:@"ClientRes.zip"];
dispatch_queue_t q = dispatch_queue_create("cn.itcast.gcddemo",DISPATCH_QUEUE_CONCURRENT);
// 拷貝壓縮包到沙盒
dispatch_sync(q, ^{
[[NSFileManager defaultManager] copyItemAtPath:eawPath toPath:eawRPath error:NULL];
});
// 解壓壓縮包
dispatch_sync(q, ^{
[self OpenZip:eawRPath unzipto:docspath];
});
// 刪除壓縮包
dispatch_sync(q, ^{
[self deletDocumentsFile:eawRPath];
});
PostToUnityOne(StartGame, nil);
}
// 解壓zip包
- (void)OpenZip:(NSString*)zipPath unzipto:(NSString*)_unzipto
{
BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:zipPath];
if (!blHave) {
NSLog(@"no have");
return ;
}else {
ZipArchive* zip = [[ZipArchive alloc] init];
if( [zip UnzipOpenFile:zipPath] )
{
BOOL ret = [zip UnzipFileTo:_unzipto overWrite:YES];
if( NO==ret )
{
NSLog(@"error");
}
[zip UnzipCloseFile];
}
}
}
// 刪除沙盒文件
-(void)deletDocumentsFile:(NSString *)filePath{
//文件名
BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (!blHave) {
NSLog(@"no have");
return ;
}else {
NSLog(@" have");
BOOL blDele= [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
if (blDele) {
NSLog(@"dele success");
}else {
NSLog(@"dele fail");
}
}
}
// 刪除緩存
-(void)deleteCaches{
// 刪除緩存
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath: cachesDir];
[files enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *pathS=[cachesDir stringByAppendingPathComponent:obj];
[self deletDocumentsFile:pathS];
}]
}
ZipArchive庫:https://pan.baidu.com/s/1bpaSFPt