'NSURLSession - download and do *not* decompress gzip?
I'm using NSURLSession's dataTaskWithRequest to download a file. It's gzipped, and it is automatically decompressed. However, I don't want it to be - I want the gzipped source. Is there any way to disable decompression?
Solution 1:[1]
NSURLSession automatically inserts this value to your request's header:
"Accept-Encoding: gzip;q=1.0, compress;q=0.5"
which causes the downloaded data to be automatically decoded. So I think you should start by replacing the value of "Accept-Encoding" with something else.
Solution 2:[2]
maybe this will reslove:
- (BOOL)download:(NSURLDownload *)download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType;
Solution 3:[3]
It all depends on the Content-Type, if the Content-Type says it's text/html, it doesn't matter whether it's zipped or not, Task will call completion with text/html bytes.
Anyhow, if you have the wrong Content-Type, and still want this, you can instance NSURLDownload (it's the legacy url loading system), implement and set a delegate (NSURLDownloadDelegate), and in your delegate implement this:
- (BOOL)download:(NSURLDownload *)download
shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType {
return NO;
}
There's no way to do it with URLSession AFAIK, except of course implementing your own protocol, but that might be too much.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | arturdev |
| Solution 2 | Justin Liu |
| Solution 3 | no_ripcord |
