开源项目objective-zip
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 else 101 [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 }
无论生活、还是技术,一切都不断的学习和更新~~~努力~