iPhone:在运行时下载压缩包并在主包子目录中解压

我想扩展我的iPhone应用程序,该应用程序将zip文件下载到子目录中,然后提取它,然后加载zip内的图像。

任何想法如何在运行时解压缩和访问图像? 会有一些想法真的很开心。

问候,

我以前使用ZipArchive成功了。

这是相当ligyweight和使用简单,支持密码保护,ZIP中的多个文件,以及压缩和解压缩。

基本用法是:

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ZipFileName" ofType:@"zip"]; ZipArchive *zipArchive = [[ZipArchive alloc] init]; [zipArchive UnzipOpenFile:filepath Password:@"xxxxxx"]; [zipArchive UnzipFileTo:{pathToDirectory} overWrite:YES]; [zipArchive UnzipCloseFile]; [zipArchive release]; 

你不能提取到你的包。 使用[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]来获取可写入目录的path。

您可以使用http://code.google.com/p/ziparchive/中的代码从zip压缩文件中提取文件。

如果您添加ZipArchive到您的项目,请记住也添加框架。 正确的框架是libz.dylib最新版本(Xcode 4.2)是1.2.5

(在目标的“构build阶段”选项卡中将框架添加到包含库的部分。)

我build议你使用ssziparchive bcoz它支持ARCNon ARC项目。

  1. SSZipArchive.hSSZipArchive.mminizip添加到您的项目。
  2. 将最新版本的libz (现在是它的libz1.2.5 )库添加到您的目标。

你不需要做任何关于ARC事情。 SSZipArchive会检测你是否使用ARC并添加所需的内存pipe理代码。

而代码将是这样的:

  NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // Unzipping //If your zip is in document directory than use this code NSString *zipPath = [documentsDirectory stringByAppendingPathComponent:@"mediadata.zip"]; //else if zip file is in bundle than use this code NSString *zipPath = [[NSBundle mainBundle] pathForResource:@"mediadata" ofType:@"zip"]; NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:@"MediaData"]; if( [SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath] != NO ) { //unzip data success //do something NSLog(@"Dilip Success"); }else{ NSLog(@"Dilip Error"); }