ios iPhone:在运行时下载 zip 并在主包子目录中解压缩
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5385480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone: Downloading zip and extracting in main bundle subdirectory at runtime
提问by Nando
I want to extend my iPhone app that the app downloads a zip file into a sub directory then extracts it and then load images which were inside the zip.
我想扩展我的 iPhone 应用程序,该应用程序将一个 zip 文件下载到一个子目录中,然后解压缩它,然后加载 zip 中的图像。
Any ideas how to unzip in run-time and the access the images? Would be really happy for some idea.
任何想法如何在运行时解压缩和访问图像?有一些想法会很高兴。
Greetings,
你好,
回答by Rog
I've used ZipArchivewith success in the past.
我过去曾成功地使用过ZipArchive。
It's pretty ligthweight and simple to use, supports password protection, multiple files inside a ZIP, as well as compress & decompress.
它非常轻巧且易于使用,支持密码保护、ZIP 中的多个文件以及压缩和解压缩。
The basic usage is:
基本用法是:
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];
回答by Anomie
You cannot extract into your bundle. Use [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
to get the path to a directory you can write to.
您无法提取到您的捆绑包中。使用[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
去你可以写一个目录的路径。
You can use the code from http://code.google.com/p/ziparchive/to extract files from the zip archive.
您可以使用http://code.google.com/p/ziparchive/ 中的代码从zip 存档中提取文件。
回答by Bjinse
If you add ZipArchive
to your project, remember to also add the framework.
The correct framework is libz.dylib
. Latest version (Xcode 4.2) is 1.2.5.
如果添加ZipArchive
到项目中,请记住还要添加框架。正确的框架是libz.dylib
. 最新版本(Xcode 4.2)是 1.2.5。
(Add framework to the section with libraries in the build phases tab of your target.)
(将框架添加到目标的构建阶段选项卡中带有库的部分。)
回答by Dilip
I suggest you to use ssziparchivebcoz it support both ARC
and Non ARC
project.
我建议您使用ssziparchivebcoz 它同时支持ARC
和Non ARC
项目。
- Add
SSZipArchive.h
,SSZipArchive.m
, andminizip
to your project. - Add latest version of the
libz
(right now itslibz1.2.5
) library to your target.
- 将
SSZipArchive.h
,SSZipArchive.m
, 和添加minizip
到您的项目中。 - 将最新版本的
libz
(现在是它的libz1.2.5
)库添加到您的目标。
You don't need to do anything regarding ARC
. SSZipArchive
will detect if you're not using ARC
and add the required memory management code.
您无需对ARC
. SSZipArchive
将检测您是否未使用ARC
并添加所需的内存管理代码。
And code will be like this:
代码将是这样的:
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");
}