博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
开源项目objective-zip
阅读量:5102 次
发布时间:2019-06-13

本文共 6822 字,大约阅读时间需要 22 分钟。

Objective-Zip is a small Cocoa/Objective-C library that wraps ZLib and MiniZip in an object-oriented friendly way.

项目地址:

http://code.google.com/p/objective-zip/

 

部分使用代码:(代码来自官网下载)

1 - (void) test {  2     NSAutoreleasePool *pool= [[NSAutoreleasePool alloc] init];  3   4     @try {  5         NSString *documentsDir= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];  6         NSString *filePath= [documentsDir stringByAppendingPathComponent:@"test.zip"];  7   8         [self performSelectorOnMainThread:@selector(log:) withObject:@"Opening zip file for writing..." waitUntilDone:YES];  9          10         ZipFile *zipFile= [[ZipFile alloc] initWithFileName:filePath mode:ZipFileModeCreate]; 11  12         [self performSelectorOnMainThread:@selector(log:) withObject:@"Adding first file..." waitUntilDone:YES]; 13          14         ZipWriteStream *stream1= [zipFile writeFileInZipWithName:@"abc.txt" fileDate:[NSDate dateWithTimeIntervalSinceNow:-86400.0] compressionLevel:ZipCompressionLevelBest]; 15  16         [self performSelectorOnMainThread:@selector(log:) withObject:@"Writing to first file's stream..." waitUntilDone:YES]; 17  18         NSString *text= @"abc"; 19         [stream1 writeData:[text dataUsingEncoding:NSUTF8StringEncoding]]; 20  21         [self performSelectorOnMainThread:@selector(log:) withObject:@"Closing first file's stream..." waitUntilDone:YES]; 22          23         [stream1 finishedWriting]; 24          25         [self performSelectorOnMainThread:@selector(log:) withObject:@"Adding second file..." waitUntilDone:YES]; 26          27         ZipWriteStream *stream2= [zipFile writeFileInZipWithName:@"x/y/z/xyz.txt" compressionLevel:ZipCompressionLevelNone]; 28          29         [self performSelectorOnMainThread:@selector(log:) withObject:@"Writing to second file's stream..." waitUntilDone:YES]; 30          31         NSString *text2= @"XYZ"; 32         [stream2 writeData:[text2 dataUsingEncoding:NSUTF8StringEncoding]]; 33          34         [self performSelectorOnMainThread:@selector(log:) withObject:@"Closing second file's stream..." waitUntilDone:YES]; 35          36         [stream2 finishedWriting]; 37          38         [self performSelectorOnMainThread:@selector(log:) withObject:@"Closing zip file..." waitUntilDone:YES]; 39          40         [zipFile close]; 41         [zipFile release]; 42          43         [self performSelectorOnMainThread:@selector(log:) withObject:@"Opening zip file for reading..." waitUntilDone:YES]; 44          45         ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:filePath mode:ZipFileModeUnzip]; 46          47         [self performSelectorOnMainThread:@selector(log:) withObject:@"Reading file infos..." waitUntilDone:YES]; 48          49         NSArray *infos= [unzipFile listFileInZipInfos]; 50         for (FileInZipInfo *info in infos) { 51             NSString *fileInfo= [NSString stringWithFormat:@"- %@ %@ %d (%d)", info.name, info.date, info.size, info.level]; 52             [self performSelectorOnMainThread:@selector(log:) withObject:fileInfo waitUntilDone:YES]; 53         } 54          55         [self performSelectorOnMainThread:@selector(log:) withObject:@"Opening first file..." waitUntilDone:YES]; 56          57         [unzipFile goToFirstFileInZip]; 58         ZipReadStream *read1= [unzipFile readCurrentFileInZip]; 59          60         [self performSelectorOnMainThread:@selector(log:) withObject:@"Reading from first file's stream..." waitUntilDone:YES]; 61          62         NSMutableData *data1= [[[NSMutableData alloc] initWithLength:256] autorelease]; 63         int bytesRead1= [read1 readDataWithBuffer:data1]; 64          65         BOOL ok= NO; 66         if (bytesRead1 == 3) { 67             NSString *fileText1= [[[NSString alloc] initWithBytes:[data1 bytes] length:bytesRead1 encoding:NSUTF8StringEncoding] autorelease]; 68             if ([fileText1 isEqualToString:@"abc"]) 69                 ok= YES; 70         } 71          72         if (ok) 73             [self performSelectorOnMainThread:@selector(log:) withObject:@"Content of first file is OK" waitUntilDone:YES]; 74         else 75             [self performSelectorOnMainThread:@selector(log:) withObject:@"Content of first file is WRONG" waitUntilDone:YES]; 76              77         [self performSelectorOnMainThread:@selector(log:) withObject:@"Closing first file's stream..." waitUntilDone:YES]; 78          79         [read1 finishedReading]; 80          81         [self performSelectorOnMainThread:@selector(log:) withObject:@"Opening second file..." waitUntilDone:YES]; 82  83         [unzipFile goToNextFileInZip]; 84         ZipReadStream *read2= [unzipFile readCurrentFileInZip]; 85  86         [self performSelectorOnMainThread:@selector(log:) withObject:@"Reading from second file's stream..." waitUntilDone:YES]; 87          88         NSMutableData *data2= [[[NSMutableData alloc] initWithLength:256] autorelease]; 89         int bytesRead2= [read2 readDataWithBuffer:data2]; 90          91         ok= NO; 92         if (bytesRead2 == 3) { 93             NSString *fileText2= [[[NSString alloc] initWithBytes:[data2 bytes] length:bytesRead2 encoding:NSUTF8StringEncoding] autorelease]; 94             if ([fileText2 isEqualToString:@"XYZ"]) 95                 ok= YES; 96         } 97          98         if (ok) 99             [self performSelectorOnMainThread:@selector(log:) withObject:@"Content of second file is OK" waitUntilDone:YES];100         else101             [self performSelectorOnMainThread:@selector(log:) withObject:@"Content of second file is WRONG" waitUntilDone:YES];102         103         [self performSelectorOnMainThread:@selector(log:) withObject:@"Closing second file's stream..." waitUntilDone:YES];104         105         [read2 finishedReading];106         107         [self performSelectorOnMainThread:@selector(log:) withObject:@"Closing zip file..." waitUntilDone:YES];108         109         [unzipFile close];110         [unzipFile release];111         112         [self performSelectorOnMainThread:@selector(log:) withObject:@"Test terminated succesfully" waitUntilDone:YES];113         114     } @catch (ZipException *ze) {115         [self performSelectorOnMainThread:@selector(log:) withObject:@"Caught a ZipException (see logs), terminating..." waitUntilDone:YES];116         117         NSLog(@"ZipException caught: %d - %@", ze.error, [ze reason]);118 119     } @catch (id e) {120         [self performSelectorOnMainThread:@selector(log:) withObject:@"Caught a generic exception (see logs), terminating..." waitUntilDone:YES];121 122         NSLog(@"Exception caught: %@ - %@", [[e class] description], [e description]);123     }124     125     [pool drain];126 }

 

 

 

转载于:https://www.cnblogs.com/GoGoagg/archive/2012/12/11/2813031.html

你可能感兴趣的文章
在linux中出现there are stopped jobs 的解决方法
查看>>
获取浏览器版本信息
查看>>
SQLServer之删除视图
查看>>
js forEach跳出循环
查看>>
MyBatis---动态SQL
查看>>
快速创建一个 spring mvc 示例
查看>>
swing入门教程
查看>>
好莱坞十大导演排名及其代表作,你看过多少?
查看>>
JVM-class文件完全解析-类索引,父类索引和索引集合
查看>>
Loj #139
查看>>
StringBuffer是字符串缓冲区
查看>>
hihocoder1187 Divisors
查看>>
java入门
查看>>
Spring 整合 Redis
查看>>
Azure 托管镜像和非托管镜像对比
查看>>
SQLite3初探
查看>>
多线程/多进程/异步IO
查看>>
leetcode 442. 数组中重复的数据 java
查看>>
struts2 文件上传下载注解示例
查看>>
编写一个简单的JAVA WEB Servlet页面
查看>>