ios 异步下载文件。

 

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值