网络多任务下载,断点下载

来源:互联网 发布:win7 禁止卸载软件 编辑:程序博客网 时间:2024/05/06 16:26

直接上代码:

#pragma mark -下载游戏-(void)loadGameZipWithUrl:(NSString *)gameUrl{       //进度缓存目录    NSString *tempPath  = [self getProcessDocument];    //下载游戏的本地地址    NSString * documentpath= [self getGamePahtUrl];    NSString* unzipfilePath =[documentpath stringByAppendingString:@".zip"];    NSFileManager *filemanager = [NSFileManager defaultManager];    //下载地址    NSURL *url = [NSURL URLWithString:gameUrl];    NSURLRequest * request = [NSURLRequest  requestWithURL:url];    unsigned long long  downloadedBytes =0;    //检查已经下载的部分    if ([filemanager fileExistsAtPath:unzipfilePath])//如果存在,说明有缓存文件    {        downloadedBytes = [self fileSizeAtPath:unzipfilePath];//计算缓存文件的大小        if(downloadedBytes>0){            NSMutableURLRequest *mutableURLRequest = [request mutableCopy];            NSString *requestRange = [NSString stringWithFormat:@"bytes=%llu-", downloadedBytes];            [mutableURLRequest setValue:requestRange forHTTPHeaderField:@"Range"];            request = mutableURLRequest;            NSLog(@"==============断点下载");        }    }    //初始化队列    NSOperationQueue *queue = [[NSOperationQueue alloc ]init];    //不使用缓存,避免断点续传出现问题    [[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];    //设置存储路径    op.outputStream = [NSOutputStream outputStreamToFileAtPath:unzipfilePath append:YES];    __block typeof(op) operation = op;    // 下载进度    [op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {        CGFloat precent =((float)totalBytesRead + downloadedBytes) / (totalBytesExpectedToRead + downloadedBytes);        //设置进度         NSLog(@"process ==>%f",precent);         NSString * progress = [NSString stringWithFormat:@"%.3f",((float)totalBytesRead + downloadedBytes) / (totalBytesExpectedToRead + downloadedBytes)];        [progress writeToFile:tempPath atomically:YES encoding:NSUTF8StringEncoding error:nil];    }];    //下载成功    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {        NSLog(@"下载成功");        NSString *stringUrl = [operation.request.URL absoluteString];             //下载完成,解压游戏,并删除zip包             [self unZipGameWithName:[NSString stringWithFormat:@"%@/%@",documentpath,XXX]];        }    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {        NSLog(@"下载失败");    }];    //开始下载//    [op start];    [queue addOperation:op];}#pragma mark-计算缓存文件大小的方法- (unsigned long long)fileSizeAtPath:(NSString *)fileAbsolutePath {    signed long long fileSize = 0;    NSFileManager *fileManager = [NSFileManager defaultManager];    if ([fileManager fileExistsAtPath:fileAbsolutePath]) {        NSError *error = nil;        NSDictionary *fileDict = [fileManager attributesOfItemAtPath:fileAbsolutePath error:&error];        if (!error && fileDict) {            fileSize = [fileDict fileSize];        }    }    return fileSize;}

获取缓存进度目录

#pragma mark-获取进度目录-(NSString *)getProcessDocument{    //进度目录    NSString *tempFile =[NSString stringWithFormat:@"%@Temp.txt",GameType_SLYZ];    tempPath=[ NSTemporaryDirectory() stringByAppendingPathComponent:tempFile];    return  tempPath;}
#pragma mark - 获取压缩后游戏地址-(NSString*)getGamePahtUrl{    //游戏的目录地址    NSString * documentpath= [self getGamePath];    NSString* gamefilePath = [documentpath stringByAppendingFormat:@"/%@",gType] ;    return  gamefilePath;}
#pragma mark - 获取压缩包游戏地址-(NSString *)getGamePath{    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;    return documentpath;}
1 0
原创粉丝点击