iOS - 下载NSURLSessionTask解压缩文件

时间:2016-04-25 06:13:53

标签: ios objective-c nsurlsession nsfilemanager

我在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数组为空。我做错了什么?

3 个答案:

答案 0 :(得分:0)

您尚未编写文件名和文件类型: -

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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