如何解压缩文件?

时间:2016-09-12 05:17:29

标签: ios objective-c zip archive unzip

我使用SSZipArchive。下载文件后,我想解压缩存档并显示图像。但代码不起作用。如何解决?

下载文件

 -(IBAction) downloadButton:(id)sender
    {
        if (_HighScore == 2) {

            _url1 =[NSURL URLWithString:@"link2.zip"];

            _downloadTask1 = [_session downloadTaskWithURL:_url1];

            [_downloadTask1 resume];
    }

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{

    if (downloadTask == _downloadTask1) {

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSURL *documentsFolder = [paths objectAtIndex:0];

        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSURL *newLocation = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/2.zip", documentsFolder]];

        NSError *error;
        [fileManager copyItemAtURL:location toURL:newLocation error:&error];
NSLog(@"file%@", newLocation.absoluteString);
}

解压缩文件

    _documentsDirectory1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    _zipPath1 = [_documentsDirectory1 stringByAppendingPathComponent:@"2.zip"];
    _destinationPath1 = [_documentsDirectory1 stringByAppendingPathComponent:@"file://%@/2.zip"];
    _fileExists1 = [[NSFileManager defaultManager] fileExistsAtPath:_zipPath1 isDirectory:false];
    if( [SSZipArchive unzipFileAtPath:_zipPath1 toDestination:_destinationPath1] != NO ) {
        NSLog(@"Dilip Success");
    }else{
        NSLog(@"Dilip Error");
    }

UPD

-(IBAction) downloadButton:(id)sender
{
    if (_HighScore == 2) {

        _url1 =[NSURL URLWithString:@"link2.zip"];

        _downloadTask1 = [_session downloadTaskWithURL:_url1];

        [_downloadTask1 resume];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{

    if (downloadTask == _downloadTask1) {
        _documentsDirectory1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        _zipPath1 = [_documentsDirectory1 stringByAppendingPathComponent:@"2.zip"];
    }
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    if (downloadTask == _downloadTask1) { NSData *urlData1 = [NSData dataWithContentsOfURL:_url1]; [urlData1 writeToFile:_zipPath1 atomically:YES];}

});
}

2 个答案:

答案 0 :(得分:1)

问题在于保存DocumentDirectory中的zip文件,_filePath1包含用于解压缩的目标文件夹,而不是需要使用包含filePath文件的zip名字和路径一起使用,所以请像这样使用。

[urlData1 writeToFile:filePath atomically:YES];

如果你使用writeToFile:options:error:方法,那么你就可以知道它是否成功写了Data

NSError *error = nil;
[self.responseData writeToFile:zipPath options:0 error:&error];

修改 您可能正在搞乱某些事情,因此请将代码从下载更改为保存和解压缩。

-(IBAction) downloadButton:(id)sender
{
    if (_HighScore == 2) {
        _url1 =[NSURL URLWithString:@"link2.zip"];  
        _downloadTask1 = [session dataTaskWithURL:[NSURL URLWithString:@""] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

            //Saved in NSDocumentDirectory
            NSError *error = nil;
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *path = [paths objectAtIndex:0];
            NSString *zipPath = [path stringByAppendingPathComponent:@"2.zip"];
            [self.responseData writeToFile:zipPath options:0 error:&error];

            //UNZip
            NSString *zipPath1 = [path stringByAppendingPathComponent:@"2.zip"];
            NSString *destinationPath = [NSString stringWithFormat:@"%@",path];
            [SSZipArchive unzipFileAtPath:zipPath1 toDestination:destinationPath];
            //Now access the content of zip
        }]; 
        [_downloadTask1 resume];
    }   
}

答案 1 :(得分:0)

尝试这样,它为我工作

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

完成取消归档后,以下方法将调用

#pragma mark - Unzipp Delegate

 - (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:   (unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPath {
       //write code here after unarchive zip file
    }