IOS 压缩与解压字符串(文件) 操作
来源:互联网 发布:网络dj歌曲最红最好听 编辑:程序博客网 时间:2024/05/16 12:32
实现对字符串进行压缩解压操作 前提需要导入libz.1.2.5.dylib 库
#import "BYViewController.h"#import "zipAndUnzip.h"@interface BYViewController ()@end@implementation BYViewController- (void)viewDidLoad{ [super viewDidLoad]; zipAndUnzip *zipAnd = [[zipAndUnzip alloc] init]; NSString *str = @"Hello.madison向显笑"; NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(NSUTF16BigEndianStringEncoding); NSData *data = [str dataUsingEncoding:enc]; NSData *dataDeflate = [zipAnd gzipDeflate:data]; Byte *byte = (Byte *)[dataDeflate bytes]; for (int i=0 ; i<[dataDeflate length]; i++){ NSLog(@"byte = %d",byte[i]); } NSData *adata = [[NSData alloc] initWithBytes:byte length:[dataDeflate length]];// NSData *dataInflate = [zipAnd gzipInflate:adata]; NSString *aString = [[NSString alloc] initWithData:adata encoding:enc]; NSLog(@"aString:%@",aString);}@end
@interface zipAndUnzip : NSObject- (NSData *)gzipInflate:(NSData*)data;- (NSData *)gzipDeflate:(NSData*)data;@end
#import "zipAndUnzip.h"#import "zlib.h"@implementation zipAndUnzip//解压缩- (NSData *)gzipInflate:(NSData*)data{ if ([data length] == 0) return data; unsigned full_length = (int)[data length]; unsigned half_length = (int)[data length] / 2; NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length]; BOOL done = NO; int status; z_stream strm; strm.next_in = (Bytef *)[data bytes]; strm.avail_in = (int)[data 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 = (uint)[decompressed length] - (uint)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]; } else return nil;}//压缩- (NSData *)gzipDeflate:(NSData*)data{ if ([data length] == 0) return data; z_stream strm; strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; strm.total_out = 0; strm.next_in=(Bytef *)[data bytes]; strm.avail_in = (int)[data length]; // Compresssion Levels: // Z_NO_COMPRESSION // Z_BEST_SPEED // Z_BEST_COMPRESSION // Z_DEFAULT_COMPRESSION if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY) != Z_OK) return nil; NSMutableData *compressed = [NSMutableData dataWithLength:16384]; // 16K chunks for expansion do { if (strm.total_out >= [compressed length]) [compressed increaseLengthBy: 16384]; strm.next_out = [compressed mutableBytes] + strm.total_out; strm.avail_out = (uInt)[compressed length] - (uint)strm.total_out; deflate(&strm, Z_FINISH); } while (strm.avail_out == 0); deflateEnd(&strm); [compressed setLength: strm.total_out]; return [NSData dataWithData:compressed];}@end
0 0
- IOS 压缩与解压字符串(文件) 操作
- iOS开发之压缩与解压文件
- 压缩与解压iOS
- 文件压缩与解压
- 文件压缩与解压
- 文件压缩与解压
- 文件压缩与解压
- 文件压缩与解压
- 文件压缩与解压
- 文件压缩与解压
- JAVA 实现文件(字符串)压缩与解压
- 对文件压缩解压操作
- 文件上传、压缩解压 操作
- 字符串的压缩与解压
- I/O操作之文件压缩与解压
- Linux下zip文件的压缩与解压操作
- 压缩与解压1---字符串的压缩与解压
- java压缩与解压文件
- iOS开发点滴-GameCenter报错问题
- Makefile中的伪目标
- 开源PaaS平台-CloudFoundry的架构设计、
- 如何用QT实现txt文件的读写
- vs2010启动调试、停止调试非常慢
- IOS 压缩与解压字符串(文件) 操作
- Java Swing 支持 撤销和恢复功能 ctrl+z ctrl+y
- [容斥原理] zoj 3556 How Many Sets I
- Nginx配置文件详细说明
- nginx tomcat负载均衡之文件上传访问策略
- 所有编程皆为WEB编程——Qt之后的路该怎么走
- 小翼的java开发俄罗斯方块--------继承的概念(2)
- Entity Framework 中 Attribute的整理 来源网络
- iOS 遇到Could not change executable permissions on the application的情况