2016-04-25 105 views
1

我在didFinishDownloadingToURL:下载了文件,我想将其解压缩。我目前的代码如下所示:iOS - 已下载NSURLSessionTask解压缩文件

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

回答

0

,则不应使用absoluteString,为包括方案(例如file://)。改为使用path方法。

0

您还没有写入的文件名和文件类型: -

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ZipFileName" ofType:@"zip"]; 
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];