/**
* GZip壓縮數(shù)據(jù)
*
* @param aUnData 未壓縮數(shù)據(jù)
*
* @return 已壓縮數(shù)據(jù)
*/
+ (NSData *)toGZipCompressData:(NSData *)aUnData {
if (![aUnData isKindOfClass:[NSData class]]) {
return nil;
}
@try {
if (aUnData.length == 0 || [self isGzippedData:aUnData]) {
return aUnData;
}
z_stream stream;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
stream.avail_in = (uint) aUnData.length;
stream.next_in = (Bytef *) (void *) aUnData.bytes;
stream.total_out = 0;
stream.avail_out = 0;
static const NSUInteger ChunkSize = 16384;
NSMutableData *output = nil;
if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY) == Z_OK) {
output = [NSMutableData dataWithLength:ChunkSize];
while (stream.avail_out == 0) {
if (stream.total_out >= output.length) {
output.length += ChunkSize;
}
stream.next_out = (uint8_t *) output.mutableBytes + stream.total_out;
stream.avail_out = (uInt)(output.length - stream.total_out);
deflate(&stream, Z_FINISH);
}
deflateEnd(&stream);
output.length = stream.total_out;
}
return output;
}
@catch (NSException *exception) {
NSLog(@"%@", exception);
return nil;
}
}
+ (BOOL)isGzippedData:(NSData *)aData {
const UInt8 *bytes = (const UInt8 *) aData.bytes;
return (aData.length >= 2 && bytes[0] == 0x1f && bytes[1] == 0x8b);
}
iOS GZip壓縮數(shù)據(jù)
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
相關閱讀更多精彩內(nèi)容
- iOS 使用 zlib 庫實現(xiàn)請求數(shù)據(jù)壓縮 1.Content-Encoding Accept-Encoding ...
- 不同數(shù)據(jù)類型與NSData互轉參考如下:NSData 類型轉換 在開發(fā)中,經(jīng)常要對比較大的數(shù)據(jù)進行壓縮后再上傳服務...
- 根據(jù)pcm文件轉MP3 (void)conventToMp3 {NSString *cafFilePath = [...
- 標題好長。 今天整理數(shù)據(jù)發(fā)現(xiàn)06年之前我的博客數(shù)據(jù)還在,都有備份,所以打算都導入到現(xiàn)在的博客里。 今天備份數(shù)據(jù)進行...