OC 对ISO-8859-1编码格式的进行GZIP解压

xiaoxiao2021-02-27  326

最近项目(印尼项目)从服务器获取数据  有7000多条  数据量很大  获取数据时间较长  这样很不友好   服务器那边做了优化  把数据进行ISO-8859-1格式压缩

这样得到的数据才50多K   是之前数据的十分之一不到  响应时间大大缩短  服务器返回的数据格式如下图

解码OC代码如下  

+ (NSMutableArray *)parseProCityAndAreacountData:(NSDictionary *)result{ int resultCode = [[result objectForKey:@"success"] intValue]; NSMutableArray *resultData; if (resultCode == 1) { NSString *testString = [result objectForKey:@"data"]; NSStringEncoding enc=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatin1); NSData *testData = [testString dataUsingEncoding:enc]; NSData *gzipData = [LFCGzipUtility ungzipData:testData]; NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:gzipData options:NSJSONReadingAllowFragments error:nil]; resultData = [[NSMutableArray alloc] initWithCapacity:dataArray.count]; for (int i=0; i<dataArray.count; i++) { NSDictionary *dic = dataArray[i]; Provice *c = [[Provice alloc] init]; c.proName = [dic objectForKey:@"province"]; c.cityName = [dic objectForKey:@"city"]; c.countyareaName = [dic objectForKey:@"countyarea"]; NSString *sectionName = [[NSString stringWithFormat:@"%c",pinyinFirstLetter([c.cityName characterAtIndex:0])] uppercaseString]; NSUInteger firstLetter = [ALPHA rangeOfString:[sectionName substringToIndex:1]].location; c.sortLetters = sectionName; c.firstLetter = [NSString stringWithFormat:@"%lu", (unsigned long)firstLetter]; [resultData addObject:c]; } } return resultData; }

解码Android代码如下

byte[] bytes = s.getBytes("ISO-8859-1"); Log.d("CityRemoteDao", "解压前大小:"+bytes.length); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(bytes)); byte[] buffer = new byte[1024 * 5]; int len = 0; while ((len = in.read(buffer)) > 0) { baos.write(buffer, 0, len); } bytes = baos.toByteArray(); Log.d("CityRemoteDao", "解压后大小:"+bytes.length); s = new String(bytes, "UTF-8");

这里要注意:

1、导入类 LFCGzipUtility.h   这个是别人写好的   类似于安卓GZIPInputStream吧   

2、上述LFCGzipUtility类需要 导入libz.dylib / libz.tbd ( for iOS9.0 or later )

LFCGzipUtility下载地址:  http://download.csdn.net/detail/lixianyue1991/9833170

转载请注明原文地址: https://www.6miu.com/read-2292.html

最新回复(0)