라이브러리.
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];
'프로그래밍 > iOS' 카테고리의 다른 글
iOS5의 UDID 정책 변경에 따른 문제해결방안 (0) | 2012.01.03 |
---|---|
이미지의 이동, 회전, 스케일 변경 (0) | 2011.12.27 |
공통으로 쓰이는 코드, Library로 만들기. (0) | 2011.12.27 |
앱 아이콘의 상단부가 shining (빛 나는) 되는 효과 없애기!! – Remove Shine / Gloss Effect on iPhone Icon (0) | 2011.12.21 |
사용자 한글 글꼴 사용하기 (0) | 2011.11.29 |