-
大小: 3KB文件類型: .zip金幣: 2下載: 0 次發(fā)布日期: 2021-06-13
- 語(yǔ)言: 其他
- 標(biāo)簽: ISO-8859-1??
資源簡(jiǎn)介
類似于安卓GZIPInputStream

代碼片段和文件信息
#import?“LFCGzipUtility.h“
?
@implementation?LFCGzipUtility
?
+(NSData*)?gzipData:?(NSData*)pUncompressedData
{
????if?(!pUncompressedData?||?[pUncompressedData?length]?==?0)
????{
????????NSLog(@“%s:?Error:?Can‘t?compress?an?empty?or?null?NSData?object.“?__func__);
????????return?nil;
????}
?
????z_stream?zlibStreamStruct;
????zlibStreamStruct.zalloc????=?Z_NULL;?//?Set?zalloc?zfree?and?opaque?to?Z_NULL?so
????zlibStreamStruct.zfree?????=?Z_NULL;?//?that?when?we?call?deflateInit2?they?will?be
????zlibStreamStruct.opaque????=?Z_NULL;?//?updated?to?use?default?allocation?functions.
????zlibStreamStruct.total_out?=?0;?//?Total?number?of?output?bytes?produced?so?far
????zlibStreamStruct.next_in???=?(Bytef*)[pUncompressedData?bytes];?//?Pointer?to?input?bytes
????zlibStreamStruct.avail_in??=?[pUncompressedData?length];?//?Number?of?input?bytes?left?to?process
?
????int?initError?=?deflateInit2(&zlibStreamStruct?Z_DEFAULT_COMPRESSION?Z_DEFLATED?(15+16)?8?Z_DEFAULT_STRATEGY);
????if?(initError?!=?Z_OK)
????{
????????NSString?*errorMsg?=?nil;
????????switch?(initError)
????????{
????????????case?Z_STREAM_ERROR:
????????????????errorMsg?=?@“Invalid?parameter?passed?in?to?function.“;
????????????????break;
????????????case?Z_MEM_ERROR:
????????????????errorMsg?=?@“Insufficient?memory.“;
????????????????break;
????????????case?Z_VERSION_ERROR:
????????????????errorMsg?=?@“The?version?of?zlib.h?and?the?version?of?the?library?linked?do?not?match.“;
????????????????break;
????????????default:
????????????????errorMsg?=?@“Unknown?error?code.“;
????????????????break;
????????}
????????NSLog(@“%s:?deflateInit2()?Error:?\“%@\“?Message:?\“%s\““?__func__?errorMsg?zlibStreamStruct.msg);
????????return?nil;
????}
?
????//?Create?output?memory?buffer?for?compressed?data.?The?zlib?documentation?states?that
????//?destination?buffer?size?must?be?at?least?0.1%?larger?than?avail_in?plus?12?bytes.
????NSMutableData?*compressedData?=?[NSMutableData?dataWithLength:[pUncompressedData?length]?*?1.01?+?12];
?
????int?deflateStatus;
????do
????{
????????//?Store?location?where?next?byte?should?be?put?in?next_out
????????zlibStreamStruct.next_out?=?[compressedData?mutableBytes]?+?zlibStreamStruct.total_out;
?
????????//?Calculate?the?amount?of?remaining?free?space?in?the?output?buffer
????????//?by?subtracting?the?number?of?bytes?that?have?been?written?so?far
????????//?from?the?buffer‘s?total?capacity
????????zlibStreamStruct.avail_out?=?[compressedData?length]?-?zlibStreamStruct.total_out;
????????deflateStatus?=?deflate(&zlibStreamStruct?Z_FINISH);
?
????}?while?(?deflateStatus?==?Z_OK?);
?
????//?Check?for?zlib?error?and?convert?code?to?usable?error?message?if?appropriate
????if?(deflateStatus?!=?Z_STREAM_END)
????{
//????????NSString?*errorMsg?=?nil;
//????????switch?(deflateStatus)
//????????{
//????????????case?Z_ERRNO:
//????????????????errorMsg?=?@“Error?occured?while?reading?file.“;
//????????????????break;
//????????????case?Z_STREAM_ERROR:
//??????????????
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2015-12-07?02:57??LFCGzipUtility-master\
?????文件?????????223??2015-12-07?02:57??LFCGzipUtility-master\LFCGzipUtility.h
?????文件????????5827??2015-12-07?02:57??LFCGzipUtility-master\LFCGzipUtility.m
?????文件?????????392??2015-12-07?02:57??LFCGzipUtility-master\README.md
評(píng)論
共有 條評(píng)論