라이브러리.
http://code.google.com/p/objective-zip/
//압축하는 법.
- (IBAction) doZip
{
NSArray *pa = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filepath2 = [pa objectAtIndex:0];
NSString *filePath = [filepath2 stringByAppendingPathComponent:@"write.zip"];
ZipFile *zipFile= [[ZipFile alloc] initWithFileName:filePath mode:ZipFileModeCreate];
ZipWriteStream *stream1= [zipFile writeFileInZipWithName:@"write.txt" fileDate:[NSDate dateWithTimeIntervalSinceNow:-86400.0] compressionLevel:ZipCompressionLevelBest];
NSString *text= @"write";
[stream1 writeData:[text dataUsingEncoding:NSUTF8StringEncoding]];
[stream1 finishedWriting];
/*
ZipWriteStream *stream2= [zipFile writeFileInZipWithName:@"x/y/z/xyz.txt" compressionLevel:ZipCompressionLevelNone];
NSString *text2= @"XYZ";
[stream2 writeData:[text2 dataUsingEncoding:NSUTF8StringEncoding]];
[stream2 finishedWriting];
*/
[zipFile close];
[zipFile release];
}
//압축 해제 하는 법
NSAutoreleasePool *pool= [[NSAutoreleasePool alloc] init];
@try {
NSArray *down_pa = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *down_filepath2 = [down_pa objectAtIndex:0];
NSString *down_filePath = [down_filepath2 stringByAppendingPathComponent:@"test.zip"];
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:down_filePath mode:ZipFileModeUnzip];
NSArray *infos= [unzipFile listFileInZipInfos];
for (FileInZipInfo *info in infos) {
NSString *fileInfo= [NSString stringWithFormat:@"- %@ %@ %d (%d)", info.name, info.date, info.size, info.level];
[self performSelectorOnMainThread:@selector(log:) withObject:fileInfo waitUntilDone:YES];
}
[unzipFile goToFirstFileInZip];
ZipReadStream *read1= [unzipFile readCurrentFileInZip];
NSMutableData *data1= [[[NSMutableData alloc] initWithLength:256] autorelease];
int bytesRead1= [read1 readDataWithBuffer:data1];
NSString *fileText1= [[[NSString alloc] initWithBytes:[data1 bytes] length:bytesRead1 encoding:NSUTF8StringEncoding] autorelease];
[self performSelectorOnMainThread:@selector(log:) withObject:@"압축풀었당." waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(log:) withObject:fileText1 waitUntilDone:YES];
} @catch (ZipException *ze) {
[self performSelectorOnMainThread:@selector(log:) withObject:@"Caught a ZipException (see logs), terminating..." waitUntilDone:YES];
NSLog(@"ZipException caught: %d - %@", ze.error, [ze reason]);
} @catch (id e) {
[self performSelectorOnMainThread:@selector(log:) withObject:@"Caught a generic exception (see logs), terminating..." waitUntilDone:YES];
NSLog(@"Exception caught: %@ - %@", [[e class] description], [e description]);
}
[pool drain];
'Developer > iOS' 카테고리의 다른 글
[IPhone] Cookie head에 추가하기 (1) | 2011.12.13 |
---|---|
[iOS] zip 파일 다운로드, 업로드 (3) | 2011.06.21 |
[iOS] 콜백 메소드 만들기/호출하기 (3) | 2011.06.16 |
[iOS]기기가 가로인지 세로인지 판단하는 방법. (2) | 2011.06.16 |
[iOS] 그림의 상하가 뒤집어지는 애니메이션 (0) | 2011.06.07 |