引用libz.dylib和第三方类库:《ZipHelper》
- (void)viewDidLoad
{
[super viewDidLoad];
//注册消息
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(callBack) name: @"back" object: nil];
//开新的线程
NSThread *thread=[[NSThread alloc] initWithTarget:self selector:@selector(threadUpdatePre) object:nil];
[thread start];
[thread release];
}
//取沙盒目录
-(NSString *) getAppStartupPath
{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
}
//调用线程事件
-(void)threadUpdatePre
{
//如果主线程中有自动回收池,线程如果要用自动回收池需另开线程池
NSAutoreleasePool *pool=[[NSAutoreleasePoolalloc] init];
//指定下载地址
NSString *urlString=[[[NSStringalloc]initWithFormat:@"http://...../test.zip"]autorelease];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *data = [NSURLConnectionsendSynchronousRequest:request returningResponse:nilerror:nil];
NSString *filePath=[[[NSString alloc]initWithFormat:@"%@", [selfgetAppStartupPath]] autorelease];
[datawriteToFile:[filePath stringByAppendingString:@"/test.zip"]atomically:YES];
//向消息中心发送完成消息
[[NSNotificationCenterdefaultCenter] postNotificationName:@"back"object:self];
[pool drain];
}
//接收消息中心消息,回调相应事件
- (void)callBack{
[selfZipToFile];
}
//解压缩
-(void)ZipToFile{
NSString *filePath=[[NSString alloc]initWithFormat:@"%@", [selfgetAppStartupPath]];
ZipArchive *zp=[[ZipArchivealloc]init];
BOOL isUpdateSucess=YES;
if ([zp UnzipOpenFile:[filePath stringByAppendingString:@"/test.zip"] Password:@"123456"]) {//带密码解压
isUpdateSucess = isUpdateSucess && [zpUnzipFileTo:filePathoverWrite:YES];
[zp UnzipCloseFile];
}elseif([zpUnzipOpenFile:[filePathstringByAppendingString:@"/test.zip"]]){//不带密码解压
isUpdateSucess = isUpdateSucess && [zpUnzipFileTo:filePathoverWrite:YES];
[zp UnzipCloseFile];
}
else{
isUpdateSucess=NO;
}
[zp release];
[filePath release];
}