`
zxs19861202
  • 浏览: 909122 次
  • 性别: Icon_minigender_1
  • 来自: 湖北—》上海
社区版块
存档分类
最新评论

ios 中gzip与unGzip

    博客分类:
  • ios
 
阅读更多

工程中添加libz.dylib

.h文件如下:

 

#import <Foundation/Foundation.h>

#import "zlib.h"

 

@interface LFCGzipUtility : NSObject{

    

    

}

 

+(NSData*)gzipData:(NSData*)pUncompressedData;  //压缩

 

+(NSData*) ungzipData:(NSData *)compressedData;  //解压缩

 

 

@end

 

.m文件如下:

 

#import "LFCGzipUtility.h"

#import "zlib.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;

            caseZ_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;

    }

    

    

    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;

        

        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:

                errorMsg = @"The stream state was inconsistent (e.g., next_in or next_out was NULL).";

                break;

            case Z_DATA_ERROR:

                errorMsg = @"The deflate data was invalid or incomplete.";

                break;

            case Z_MEM_ERROR:

                errorMsg = @"Memory could not be allocated for processing.";

                break;

            case Z_BUF_ERROR:

                errorMsg = @"Ran out of output buffer for writing compressed bytes.";

                break;

            caseZ_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: zlib error while attempting compression: \"%@\" Message: \"%s\"", __func__, errorMsg, zlibStreamStruct.msg);

        

        // Free data structures that were dynamically created for the stream.

        deflateEnd(&zlibStreamStruct);

        

        return nil;

    }

    // Free data structures that were dynamically created for the stream.

    deflateEnd(&zlibStreamStruct);

    [compressedData setLength: zlibStreamStruct.total_out];

    NSLog(@"%s: Compressed file from %d KB to %d KB", __func__, [pUncompressedData length]/1024, [compressedData length]/1024);

    

    return compressedData;

}

 

 

 

+(NSData *)ungzipData:(NSData *)compressedData

{

    if ([compressedData length] == 0)

        return compressedData;

    

    unsigned full_length = [compressedData length];

    unsigned half_length = [compressedData length] / 2;

    

    NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length];

    BOOL done = NO;

    int status;

    

    z_stream strm;

    strm.next_in = (Bytef *)[compressedData bytes];

    strm.avail_in = [compressedData length];

    strm.total_out = 0;

    strm.zalloc = Z_NULL;

    strm.zfree = Z_NULL;

    if (inflateInit2(&strm, (15+32)) != Z_OK)

        return nil;

    

    while (!done) {

        // Make sure we have enough room and reset the lengths.

        if (strm.total_out >= [decompressed length]) {

            [decompressed increaseLengthBy: half_length];

        }

        strm.next_out = [decompressed mutableBytes] + strm.total_out;

        strm.avail_out = [decompressed length] - strm.total_out;

        // Inflate another chunk.

        status = inflate (&strm, Z_SYNC_FLUSH);

        if (status == Z_STREAM_END) {

            done = YES;

        } else if (status != Z_OK) {

            break;

        }

    }

    

    if (inflateEnd (&strm) != Z_OK)

        return nil;

    // Set real length.

    if (done) {

        [decompressed setLength: strm.total_out];

        return [NSData dataWithData: decompressed];

    }

    returnnil;

}

 

 

 

 

 

 

@end

 

 

分享到:
评论

相关推荐

    SharpCompressDll0.24.zip

    SharpCompress is a compression library for NET Standard 1.0 that can unrar, decompress 7zip, decompress xz, zip/unzip, tar/untar lzip/unlzip, bzip2/unbzip2 and gzip/ungzip with forward-only reading ...

    Java压缩Gzip

    在sun公司发布的jdk中包含了java.util.zip包,对gzip提供了直接支持.使用java语言可以很方便的实现gzip压缩和解压缩.以下是一个压缩和解压缩的简程序.  (压缩:gzip.java) import java.io.*; import java.util....

    文件压缩与解压缩实践docx

    3.3 程序中各个类的初步定义 4 详细设计和实现 4.1 压缩的程序流程 4.2 解压缩的程序流程 4.3 主函数代码 4.3.1 gzip压缩模块代码 4.3.2 ungzip解压缩模块代码 4.4 程序界面设计 5 软件系统测试 5.1 运行...

    文件压缩与解压缩实践(源代码+论文)

    3.3 程序中各个类的初步定义 4 详细设计和实现 4.1 压缩的程序流程 4.2 解压缩的程序流程 4.3 主函数代码 4.3.1 gzip压缩模块代码 4.3.2 ungzip解压缩模块代码 4.4 程序界面设计 5 软件系统测试 5.1 运行...

    sketch_coding:编码草图应用程序(开发中)

    前端堆栈的学习项目主要使用电子打字稿并进行React(当前为...添加用于gzip和ungzip的js zlib工具 添加进行vsc控制 为材质用户界面添加 -ui自动完成选择控制 添加 | 用于代码编辑和显示 Ctrl-F / Cmd-F Start searchin

    C#实现解压GZip文件的方法

    本文实例讲述了C#实现解压GZip文件的方法。分享给大家供大家参考。具体实现方法如下: public void ungzip(string path, string decomPath, bool overwrite) { //for overwriting purposes if (File.Exists...

    JAVA程序实现文件压缩与解压缩-项目源代码-说明-论文

    运用LZ77字典算法、懒惰匹配算法和Huffman编码算法,使用Java语言在Jbuilder2006环境下设计了使用GZIP算法对文件压缩与解压缩的实现程序。用户可以根据自己的需求,使用此程序方便地对文件进行压缩或解压缩。 程序...

    Linux:linux下解压tar.xz、tar、tar.gz等文件方法

    Linux:linux下解压tar.xz、tar、tar.gz等文件方法 1、tar语法 tar(选项)(参数) ...-z或–gzip或–ungzip:通过gzip指令处理备份文件; -Z或–compress或–uncompress:通过compress指令处理备份文件; -f或–f

    解析PHP中的file_get_contents获取远程页面乱码的问题

    PHP的file_get_contents获取远程页面内容,如果是gzip编码过的,返回的字符串就是编码后的乱码1、解决方法,找个ungzip的函数来转换下2、给你的url加个前缀,这样调用$content = file_get_contents&#40;“compress....

    sharpcompress:SharpCompress是一个完全托管的C#库,可以处理多种压缩类型和格式

    夏普压缩 SharpCompress是用于.NET Standard 2.0、2.1,.NET Core 3.1和.NET... Tar通常与单个存档中的多个文件结合使用(例如.tar.gz ) Zip可以,但是它是一种很容易发生危险的格式,并且标头和实现的变体使得难以正

    lotus-openrpc-client:从OpenRPC定义生成的Typescript中的Lotus API客户端

    Lotus-openrpc-client 从OpenRPC定义生成的Typescript中的Lotus API客户端。 从使用 :warning: 目前,这是在探索什么是可能的。 不用于消费。 :warning:出处从Lotus仓库中获取openrc定义,将其解压缩,然后将其传递...

    gun tar for windows

    -z, --gzip, --ungzip filter the archive through gzip -Z, --compress, --uncompress filter the archive through compress --use-compress-program=PROG filter through PROG (must accept -d) Local file ...

Global site tag (gtag.js) - Google Analytics