在iOS中解压缩文件

时间:2015-01-21 07:02:30

标签: ios objective-c ziparchive

我有一个包含一些图片的.zip文件夹。我希望它解压缩到iOS设备中的Documents目录。我尝试了以下链接,

download and unzip file in iOS-Stack Overflow 这是我在我的应用中尝试的代码,

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask,
                                                     YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *filepath = [documentsDirectory stringByAppendingPathComponent:@"Obj.zip"];
ZipArchive *zipArchive = [[ZipArchive alloc] init];
[zipArchive UnzipOpenFile:filepath Password:@""];
[zipArchive UnzipFileTo:[documentsDirectory stringByAppendingPathComponent:@"Obj"] overWrite:YES];
[zipArchive UnzipCloseFile];

然后我在Document Directory中打印了所有文件,但Obj.zip是文档目录中唯一存在的文件(Obj.zip是要解压缩的文件)。我怎么能这样做?

提前致谢!

1 个答案:

答案 0 :(得分:8)

使用SSZipArchive。这是一个示例代码。 https://github.com/soffes/ssziparchive

另外我相信解压缩会在文档目录中创建一个包含文件的文件夹。请检查

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];