User User - 2 months ago 25
Objective-C Question

How to unzip file?

I use

SSZipArchive
. After downloading file I want to unzip archive and show image. But code doesn’t work. How to fix it?

download file

-(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);
}


unzip file

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

});
}

Answer

The problem is in saving zip file in DocumentDirectory, _filePath1 is contain the destination folder for unzip, instead of that you need to use filePath that contain zip file name along with path, so use that like this.

[urlData1 writeToFile:filePath atomically:YES];

Also it is batter if you use writeToFile:options:error: method so that you can know it is successfully writing Data or not.

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

Edit: You are probably messing with something, so change your code from downloading to saving and unzipping like this.

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