gzip格式解压缩

简介:

gzip格式解压缩

 

有时候网络请求中会出现gzip格式的数据,而我们无法通过常规办法进行解析:

 

这时候可以使用下面的这个工具来解决这个问题:

https://github.com/mattt/Godzippa

 

注意,使用的时候需要引入一个lib:

 

本人写好的示例代码:

https://github.com/YouXianMing/iOS-Utilities



//
//  ViewController.m
//  GZipDataCompression
//
//  Created by YouXianMing on 16/3/12.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "Godzippa.h"
#import "NSData+JSONData.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    // https://github.com/mattt/Godzippa
    
    /*
     Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: application/x-gzip" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7fd8f25293e0>{ status code: 200, headers {
     Date = "Sat, 12 Mar 2016 03:48:00 GMT";
     Server = "Apache-Coyote/1.1";
     "Transfer-Encoding" = Identity;
     } }, com.alamofire.serialization.response.error.data=<1f8b0800 00000000 0000ab56 4ace4f49 55b2520a 08720d34 30303054 d2514a49 2c4954b2 aaaed551 ca2d4e07 4a3ded6f 7a367543 746671ee f33dd39e f66f8f7d b2a3f745 f3de273b 763d5fb9 0ba8bea4 b2006482 9b522d00 1076388e 4e000000>, NSLocalizedDescription=Request failed: unacceptable content-type: application/x-gzip}
     */
    
    NSURL  *fileURL  = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"GZipData" ofType:nil]];
    NSData *GZipData = [NSData dataWithContentsOfURL:fileURL];
    NSLog(@"%@ %@", GZipData, [GZipData toListProperty]);
    
    NSData *decompressingData = [GZipData dataByGZipDecompressingDataWithError:nil];
    NSLog(@"%@ %@", decompressingData, [decompressingData toListProperty]);
}

@end


目录
相关文章
|
28天前
|
Unix Linux
gzip/gunzip命令
`gzip` 和 `gunzip` 是 Linux/Unix 的压缩工具,用于压缩(`.gz`)和解压缩文件。`gzip` 使用 LZ77 和 Huffman 编码,支持递归压缩目录、设置压缩级别及保留源文件等选项。例如:`gzip file` 压缩文件,`gzip -d` 或 `gunzip` 解压缩。用户常通过调整选项完成所需操作,如`gzip -k`压缩后保留原文件。
26 4
|
8天前
|
算法 Linux
Gzip是一种广泛使用的文件压缩程序
【4月更文挑战第28天】Gzip是一种广泛使用的文件压缩程序
22 2
|
机器学习/深度学习 C#
C#使用Gzip解压缩完整读取网页内容
using System; using System.Threading; using System.Text; using System.Text.RegularExpressions; using System.
1537 0
|
JSON Java 应用服务中间件