如何获取在iphone中下载的zip文件名

lif*_*son 2 iphone zip objective-c nsurlconnection

目前使用SSZipArchive方法下载zip文件并在Documents目录文件夹中解压缩.我正在从URL下载zip文件名,目前不知道文件名,因为每次有任何更新时它都会更改.如何在收到数据时检索文件名?

- (void)viewDidLoad {
   fileManager = [NSFileManager defaultManager];

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   directoryPath = [paths objectAtIndex:0];
   filePath = [NSString stringWithFormat:@"%@/ZipFiles.zip", directoryPath];

    NSURL *url=[NSURL URLWithString:@"http://www.abc.com/test/test.cfc?id=123"];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:url];
   [request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

[fileManager createFileAtPath:filePath contents:urlData attributes:nil];
[SSZipArchive unzipFileAtPath:filePath toDestination:directoryPath];

 NSString *responseString = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];


 ///***Answer to my own question for future needs****//
  NSString *fileName = [response suggestedFilename];

}
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 5

NSURLResponse有一个方法- (NSString *)suggestedFilename,它将尝试按此顺序获取文件名.

  1. 使用内容处置标头指定的文件名.
  2. URL的最后一个路径组件.
  3. URL的主机.

内容处置头将是最好的解决方案,以便确保该服务器设置它.