gpt4 book ai didi

ios - 无法下载 gzip 文件的 bin 文件

转载 作者:行者123 更新时间:2023-11-28 23:54:56 24 4
gpt4 key购买 nike

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

但是,将发生网络连接丢失错误。你能告诉我如何解决这个问题吗?

服务器端文件名: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];

最佳答案

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.
}

关于ios - 无法下载 gzip 文件的 bin 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51397975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com