1

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?

imstillalive
  • 369
  • 4
  • 14

3 Answers3

0

You have not written the file name and file type :-

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ZipFileName" ofType:@"zip"];
0

The unzipping would create a folder in documents directory which would contain the files. Please check that

NSString *zipPath = [[NSBundle mainBundle] pathForResource:zipFileName ofType:@"zip"];
NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];

Get zip file from documents directory:-

NSString *filename = @"MyZipFile.zip";
NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * zipPath = [documentsDirectory stringByAppendingPathComponent:filename];

NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
0

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

Rob
  • 415,655
  • 72
  • 787
  • 1,044