【问题标题】:can't download bin file for gzip file无法下载 gzip 文件的 bin 文件
【发布时间】:2018-12-26 03:14:40
【问题描述】:

我在尝试使用session.dataTaskWithRequest 从 URL 读取 gzip 的函数时遇到问题。 服务器端将.gzip 更改为.bin 并存储文件。 我想用.bin 读取文件。但是,网络连接已丢失。

但是,会出现The network connection was lost 错误。 你能告诉我如何解决这个问题吗?

服务器端文件名:xxx.bin(这个bin文件是一个gzip文件。)

按照代码:

NSURLSessionConfiguration *config = [NSURLSessionConfiguration 
defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

NSURL *url = [NSURL URLWithString:@"http://...../xxx.bin"];

NSURLSessionDataTask *task = [session dataTaskWithURL: url
    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error) {
            NSLog(@"error [%@]", [error localizedDescription]);

        }
        else {
            NSLog(@"success");

        }
    }];

[task resume];

【问题讨论】:

    标签: ios objective-c nsurlsession nsurlsessiondownloadtask


    【解决方案1】:
    You can try two steps of codes to download and extract zip files.   
    
     //Fetching zip file from server
        -(void)dataSyncFileDonload{
            NSString *stringURL = [NSString stringWithFormat:@"http://yourzipfilecontainedURL"];
            stringURL =  [stringURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
            NSURL  *url = [NSURL URLWithString:stringURL];
            NSData *urlData = [NSData dataWithContentsOfURL:url];
            [self extractDownloadedZipFile:urlData];
        }
        //File Extraction from Zip file
        -(void)extractDownloadedZipFile:(NSData *)data {
    
            //If you are using SQlite and storing in local directory for extraction
            NSData *outputData = [data gunzippedData];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *path = [paths  objectAtIndex:0];
            NSString *dataPath = [path stringByAppendingPathComponent:@"myAppDB.sql"];
            NSString *dataPath1 = [path stringByAppendingPathComponent:@"myAppDB.sql.gz"];
            dataPath = [dataPath stringByStandardizingPath];
            [outputData writeToFile:dataPath atomically:YES];
            [data writeToFile:dataPath1 atomically:YES];
            [self readExtractedFile:dataPath];
        }
    
        //Read upzip file
        -(void)readExtractedFile:(NSString *)filepath loaderPercent:(float)loaderPercent loaderTo:(float)loaderTo{
            NSData *fileData = [NSData dataWithContentsOfFile:filepath];//destinationPath
            NSString *fileString = [[NSString alloc] initWithData:fileData encoding:NSASCIIStringEncoding];
            NSArray* allLinedStrings = [fileString componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];
            //You can use you data now.
        }
    

    【讨论】:

    • 您好,感谢您的评论。我立刻尝试了你的建议。但是urlData::length的大小是0。服务器端文件名:xxx.bin(这个bin文件是gzip文件)你知道这个问题怎么处理吗?
    猜你喜欢
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多