2016-04-25 103 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];