DownloadDemoDataRequest -----》ITTASIBaseDataRequest---》ITTBaseDataRequest
///文件下载请求,能同时下载多个。。。。
- (IBAction)startDownloadRequest:(id)sender
{
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t queueGroup = dispatch_group_create();
dispatch_group_async(queueGroup, aQueue, ^{
NSString *filePath = ITTPathForCacheResource(@"download_test.zip");
[DownloadDemoDataRequestrequestWithParameters:nil
withIndicatorView:nil
withCancelSubject:@"DownloadDemoDataRequestCancel"
withFilePath:filePath
onRequestFinished:^(ITTBaseDataRequest *request) {
ITTDINFO(@"DownloadDemoDataRequest finished");
_progressLabel.text = @"done";
}
onProgressChanged:^(ITTBaseDataRequest *request, float progress) {
ITTDINFO(@"DownloadDemoDataRequest progress changed:%2.2f",progress);
_progressLabel.text = [NSString stringWithFormat:@"%2.2f%@",progress*100,@"%"];
}
];
[DownloadDemoDataRequestrequestWithParameters:nil
withIndicatorView:nil
withCancelSubject:@"DownloadDemoDataRequestCancel"
withFilePath:filePath
onRequestFinished:^(ITTBaseDataRequest *request) {
ITTDINFO(@"DownloadDemoDataRequest finished");
self.firstLabel.text = @"done";
}
onProgressChanged:^(ITTBaseDataRequest *request, float progress) {
ITTDINFO(@"DownloadDemoDataRequest progress changed:%2.2f",progress);
self.firstLabel.text = [NSString stringWithFormat:@"%2.2f%@",progress*100,@"%"];
}
];
[DownloadDemoDataRequestrequestWithParameters:nil
withIndicatorView:nil
withCancelSubject:@"DownloadDemoDataRequestCancel"
withFilePath:filePath
onRequestFinished:^(ITTBaseDataRequest *request) {
ITTDINFO(@"DownloadDemoDataRequest finished");
self.secondLabel.text = @"done";
}
onProgressChanged:^(ITTBaseDataRequest *request, float progress) {
ITTDINFO(@"DownloadDemoDataRequest progress changed:%2.2f",progress);
self.secondLabel.text = [NSString stringWithFormat:@"%2.2f%@",progress*100,@"%"];
}
];
});
//任务5
// dispatch_group_async(queueGroup, aQueue, ^{
//
//
//
// });
DownloadDemoDataRequest.m 里面。。。。。
#import "DownloadDemoDataRequest.h"
#import "ITTGobalPaths.h"
#import "ZipFile.h"
#import "ZipException.h"
#import "FileInZipInfo.h"
#import "ZipWriteStream.h"
#import "ZipReadStream.h"
#import "CommonUtils.h"
#import "NSDate+ITTAdditions.h"
@implementation DownloadDemoDataRequest
- (NSString*)getRequestUrl
{
NSString *dateStr = [[NSDate date] stringWithFormat:@"yyyy-MM-dd"];
return [NSStringstringWithFormat:@"http://cn.wsj.com/ipad/plist/%@.zip",dateStr];
}
- (BOOL)processDownloadFile
{
@autoreleasepool {
BOOL success = YES;
@try {
ZipFile *unzipFile= [[ZipFilealloc] initWithFileName:_filePathmode:ZipFileModeUnzip];
NSArray *infos= [unzipFile listFileInZipInfos];
[unzipFile goToFirstFileInZip];
int index = 0;
do{
ZipReadStream *read= [unzipFile readCurrentFileInZip];
FileInZipInfo *info = infos[index];
NSString *path = ITTPathForCacheResource(@"download_unziped");;
if (info.size == 0) {
//is folder
[CommonUtils createDirectorysAtPath:path];
}else{
NSString *directorPath = [CommonUtils getDirectoryPathByFilePath:path];
[CommonUtils createDirectorysAtPath:directorPath];
NSMutableData *fileData= [NSMutableData data];
BOOL isEndofFile = NO;
while (!isEndofFile) {
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];
if (bytesRead <= 0) {
isEndofFile = YES;
}else{
[fileData appendData:data];
}
}
[fileData writeToFile:path atomically:YES];
//ITTDINFO(@"write to file [%d]:%@", success,path);
}
[read finishedReading];
index ++;
}while ([unzipFile goToNextFileInZip]);
[unzipFile close];
ITTDINFO(@"zip file unzipped...............");
} @catch (ZipException *ze) {
ITTDERROR(@"ZipException caught: %d - %@", ze.error, [ze reason]);
success = NO;
} @catch (id e) {
ITTDERROR(@"Exception caught: %@ - %@", [[e class] description], [e description]);
success = NO;
}
return success;
}
}
@end