如何解压缩文件?

How to unzip file?

我用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];}

});
}

这样试试,对我有用

[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
    }

问题是在 DocumentDirectory 中保存 zip 文件时,_filePath1 包含解压缩的目标文件夹,而不是您需要使用包含 [=16= 的 filePath ] 文件名和路径,所以像这样使用它。

[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];
    }   
}