1

我正在使用以下代码使用 ZipArchive 解压缩文件:

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 文件”。无法弄清楚为什么文件没有被解压缩。任何帮助,将不胜感激

提前致谢。

4

3 回答 3

1

首先,使用 zip delegate 来监听错误信息

@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

如果您知道错误消息,则可以检查失败的原因。但是,常见的问题是存档或目标目录不存在,或者您没有在那里写入的权限。

于 2013-05-07T09:34:05.373 回答
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;}
于 2013-05-07T07:11:03.533 回答
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];

}

希望它可以帮助你。

于 2013-05-07T07:06:58.777 回答