Translate

Thursday, July 12, 2012

iPhone Unzip code

Download unzip code : http://code.google.com/p/ziparchive/downloads/detail?name=ZipArchive.zip


#import "ZipArchive.h"



//NSString *bString=[self unzipAndSaveFileNamed:@"fileName.zip"];



- (NSString*)unzipAndSaveFileNamed:(NSString*)fileName{
NSString *strPath=nil;
ZipArchive* za = [[ZipArchive alloc] init];
@try {
NSString *fileName1=[fileName lowercaseString];
NSArray *bookNamea=[fileName1 componentsSeparatedByString:@".zip"];
NSString *bookName=[bookNamea objectAtIndex:0];
NSString *FilePath1=[NSString stringWithFormat:@"%@/%@",[DocumentDirectory documentDirectoryPath],fileName];
// NSLog(@"%@", fileName);
//NSLog(@"unzipping %@", epubFilePath);
if( [za UnzipOpenFile:FilePath1]){
strPath=[NSString stringWithFormat:@"%@/library/%@",[DocumentDirectory libraryDirectoryPath],bookName];
NSLog(@"%@", strPath);
//Delete all the previous files
NSFileManager *filemanager=[NSFileManager defaultManager];
if ([filemanager fileExistsAtPath:strPath]) {
NSError *error;
[filemanager removeItemAtPath:strPath error:&error];
}
//start unzip
BOOL ret = [za UnzipFileTo:[NSString stringWithFormat:@"%@/",strPath] overWrite:YES];
if( NO==ret ){
// error handler here
              
}
[za UnzipCloseFile];
}
}
@catch (NSException * e) {
NSLog(@"unzip Caught %@: %@", [e name], [e reason]); 
}
@finally {
[za release];
}

return strPath;
}

No comments:

Post a Comment