-
大小: 3KB文件类型: .zip金币: 1下载: 0 次发布日期: 2021-06-13
- 语言: 其他
- 标签: ISO-8859-1
资源简介
类似于安卓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:
//
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 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
- 上一篇:基于FGPA的ADC0809接口电路及程序设计.
- 下一篇:HDB3编译码器设计
评论
共有 条评论