Search code examples
iosobjective-cnsurlsessionnsfilemanager

iOS - NSURLSessionTask unzip file downloaded


I have files download at didFinishDownloadingToURL: and I want to unzip it. My current code looks like this:

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
    for(NSMutableDictionary *downloadInfo in downloadingArray)
    {
        if([[downloadInfo objectForKey:kMZDownloadKeyTask] isEqual:downloadTask])
        {
            if (location)
            {
                NSString *srcPath = location.absoluteString;
                NSString *fullPathDst = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

                //unzip
                ZipArchive *zipArchive = [[ZipArchive alloc] init];
                [zipArchive UnzipOpenFile:srcPath Password:@"pasword"];
                [zipArchive UnzipFileTo:fullPathDst overWrite:YES];
                [zipArchive UnzipCloseFile];

                NSFileManager *fileManager = [NSFileManager defaultManager];
                NSError *error;
                NSArray *files = [fileManager contentsOfDirectoryAtPath:fullPathDst error:&error];

                NSLog(@"files: %@", files);
            }

            break;
        }
    }
}

the files array is empty. What am I doing wrong?


Solution

  • You shouldn't use absoluteString, as that includes a scheme (e.g. file://). Use path method, instead.