OC 對ISO-8859-1編碼格式的進行GZIP解壓
阿新 • • 發佈:2019-01-11
最近專案(印尼專案)從伺服器獲取資料 有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