【发布时间】:2011-03-22 00:46:09
【问题描述】:
我想扩展我的 iPhone 应用程序,该应用程序下载一个 zip 文件到 一个子目录然后将其解压缩,然后加载 zip 中的图像。
任何想法如何在运行时解压缩和访问图像?会很高兴有一些想法。
您好,
【问题讨论】:
-
主包是只读的。您无法将文件提取到其中,但是您可以使用本地目录,例如文档目录。
我想扩展我的 iPhone 应用程序,该应用程序下载一个 zip 文件到 一个子目录然后将其解压缩,然后加载 zip 中的图像。
任何想法如何在运行时解压缩和访问图像?会很高兴有一些想法。
您好,
【问题讨论】:
我过去曾成功使用过ZipArchive。
它非常轻巧且易于使用,支持密码保护、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] 获取您可以写入的目录的路径。
您可以使用http://code.google.com/p/ziparchive/ 中的代码从 zip 存档中提取文件。
【讨论】:
如果您将ZipArchive 添加到您的项目中,请记住还要添加框架。
正确的框架是libz.dylib。 最新版本(Xcode 4.2)是 1.2.5。
(将框架添加到目标的构建阶段选项卡中包含库的部分。)
【讨论】:
我建议你使用ssziparchive,因为它同时支持ARC 和Non ARC 项目。
SSZipArchive.h、SSZipArchive.m 和minizip 添加到您的项目中。libz(现在是libz1.2.5)库添加到您的目标。您无需对ARC 执行任何操作。 SSZipArchive 会检测你是否没有使用ARC 并添加所需的内存管理代码。
代码会是这样的:
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");
}
【讨论】: