iOS - NSURLSessionTask 解压文件已下载

iOS - NSURLSessionTask unzip file downloaded

我在 didFinishDownloadingToURL: 下载了文件,我想解压它。我当前的代码如下所示:

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

files 数组为空。我做错了什么?

你没有写文件名和文件类型:-

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

解压缩将在包含文件的文档目录中创建一个文件夹。请检查

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

从文档目录获取 zip 文件:-

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

您不应使用 absoluteString,因为它包含一个方案(例如 file://)。请改用 path 方法。