Developer/iOS

[iOS]파일 압축, 압축 해제

블로blow 2011. 6. 21. 15:02
728x90

라이브러리.
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];

728x90