2013-05-07 49 views
1

使用ZipArchive我使用下面的代码使用ZipArchive解压缩文件的文件:解压在iOS版SDK

NSString *zipDirectoryName = [sourceURL.lastPathComponent substringWithRange:NSMakeRange(0, sourceURL.lastPathComponent.length - sourceURL.pathExtension.length - 1)]; 
NSString *zipDirectoryPath = [documentDirectory stringByAppendingPathComponent:zipDirectoryName]; 
NSLog(@"Unzipping file at path %@", zipDirectoryPath); 

success = [zip UnzipFileTo:zipDirectoryPath overWrite:YES]; 
if (!success) { 
    NSLog(@"Failed to unzip the zip file"); 
    return; 
} 
else { 
    NSLog(@"Done"); 
} 

,但我得到“无法解压缩zip文件”。无法弄清楚为什么文件没有被解压缩。任何帮助,将不胜感激

在此先感谢。从Here

+0

请参考这个链接,请确保你遵循所有步骤正确。个人认为我们的代码应该工作: 'http://www.icodeblog.com/2012/08/13/unzipping-files-using-zip -archive /' 你用过这个:'ZipArchive * za = [[ZipArchive alloc] init]; ' – 2013-05-07 08:42:57

+0

添加更多代码(如何打开存档)和“NSLog”输出将非常有帮助。 – Sulthan 2013-05-07 11:16:58

回答

0

下载SSZip然后用SSZipArchive为:

- (void)UnzippingZipFile { 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *outputPath = [documentsDirectory stringByAppendingPathComponent:@"/FolderName"]; 

    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"ZipFile.zip"]; 
    NSString *zipPath = filePath; 

    [SSZipArchive unzipFileAtPath:zipPath toDestination:outputPath delegate:self]; 

} 

希望它可以帮助你。

+0

为什么要使用SSZip?不会,ZipArchive的工作? zipArchive是否存在一些问题? – Anil 2013-05-07 07:08:40

+0

我已经使用过多次SSZipArchive。它对我来说工作得很好。 – 2013-05-07 07:11:08

+0

我已经使用minizip,它适用于我:D – 2013-05-07 08:16:29

0

试试这个:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{ 
    if (url != nil && [url isFileURL]) { 
     FileNameStr = [url.lastPathComponent substringWithRange:NSMakeRange(0, url.lastPathComponent.length - url.pathExtension.length - 1)]; 
    } 
} 

- (void)unzipAndSaveFile{ 

    NSString*EpubFile=[NSString stringWithFormat:@"%@/%@.epub",[self applicationDocumentsDirectory],FileNameStr]; 

    ZipArchive* za = [[ZipArchive alloc] init]; 

    if([za UnzipOpenFile:EpubFile]){ 

     NSString *strPath=[NSString stringWithFormat:@"%@/Unzipped/%@",[self applicationDocumentsDirectory],FileNameStr]; 

     //Delete all the previous files 
     NSFileManager *filemanager=[[NSFileManager alloc] init]; 
     if ([filemanager fileExistsAtPath:strPath]) { 

      [filemanager removeItemAtPath:strPath error:&error]; 
     } 

     [filemanager release]; 
     filemanager=nil; 

     //start unzip 
     BOOL ret = [za UnzipFileTo:[NSString stringWithFormat:@"%@/",strPath] overWrite:YES]; 

     if(NO==ret){ 
      // error handler here 
      UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error" 
                  message:@"An unknown error occured" 
                 delegate:self 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
      [alert show]; 

      [alert release]; 

      alert=nil; 
     } 

     [za UnzipCloseFile]; 
    } 

    [za release]; 
} 
- (NSString *)applicationDocumentsDirectory { 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
NSLog(@"basePath:%@",basePath); 
return basePath;} 
+0

试过......没有帮助:( – Anil 2013-05-07 09:01:38

+1

上面的代码对我来说工作正常 – Ashini 2013-05-07 09:15:33

+0

雅..也许你正确... bt不知道为什么它不适用于我:( – Anil 2013-05-07 11:08:55

1

首先,用拉链委托来听。如果你知道错误消息错误消息

@interface MyClass : NSObject <ZipArchiveDelegate> 
... 
@end 


@implementation MyClass 

- (void)ErrorMessage:(NSString*)msg { 
    NSLog(@"Zip error message: \"%@\"", msg); 
} 

- (void)doSomething { 
    ... 

    zip.delegate = self; 
    [zip UnzipFileTo:zipDirectoryPath overWrite:YES] 

    ... 
} 

@end 

,你可以检查失败的原因。但是,常见的问题是归档或目标目录不存在,或者您没有写入权限。