我已经开始使用ZipArchive了,因为我有一个测试应用程序,用于显示内容经常更改的服务器中的图像。我希望应用程序能够对图像进行排序和显示,而不必显式列出新图像名称。 (或者,也许我还应该在压缩文件中包含一个包含新图像名称的XML文档。)无论哪种方式,我现在遇到了一个问题,即将已知图像添加到服务器上的zip文件中,并在应用程序。如何在iOS中解压缩时列出未知文件?
原来zipfile.zip文件有2个文件:photo.png和的text.txt
工程。所以我下载了zipfile.zip并上传到我的服务器。当然,它仍然有效。 然后我解压缩它,添加一个新的图像,并将其重新压缩到我的服务器上。
更新zipfile.zip现在有3个文件:photo.png,myphoto.png和的text.txt
我说,myphoto.png的图像,无法找到。我错过了什么?
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSURL *url = [NSURL URLWithString:@"http://www.icodeblog.com/wp-content/uploads/2012/08/zipfile.zip"];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];
if(!error) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
NSString *zipPath = [cachePath stringByAppendingPathComponent:@"zipfile.zip"];
[data writeToFile:zipPath options:0 error:&error];
if(!error) {
ZipArchive *za = [[ZipArchive alloc] init];
if ([za UnzipOpenFile: zipPath]) {
BOOL ret = [za UnzipFileTo: cachePath overWrite: YES];
if (NO == ret){
}
[za UnzipCloseFile];
// NSString *imageFilePath=[cachePath stringByAppendingPathComponent:@"photo.png"];
NSString *imageFilePath=[cachePath stringByAppendingPathComponent:@"myphoto.png"];
NSData *imageData = [NSData dataWithContentsOfFile:imageFilePath options:0 error:nil];
if (imageData) {
NSLog(@"found data");
} else {
NSLog(@"no data");
}
UIImage *img = [UIImage imageWithData:imageData];
NSString *textFilePath = [cachePath stringByAppendingPathComponent:@"text.txt"];
NSString *textString = [NSString stringWithContentsOfFile:textFilePath encoding:NSASCIIStringEncoding error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = img;
self.label.text = textString;
});
}
} else {
NSLog(@"Error saving file %@",error);
}
} else {
NSLog(@"Error downloading zip file: %@", error);
}
});
}
当我运行这个,寻找我添加的图像,它返回“无数据”。为什么?更大的问题是:我可以在我的iOS应用程序中列出解压缩文件的内容吗?
我开始这个项目与this question其中提到这个代码:SSZipArchive。它运作良好。
进一步的测试表明,即使我将图像和文本文件更改为原始名称,并用原始文件名重新压缩它们,应用程序仍会返回“无数据”。我开始认为问题在于压缩,尽管在Mac上解压缩时我无法检测到任何奇怪的问题。 –
P.S.您的http://的zip文件。 icodeblog .com /.../ zipfile只包含2个文件...但我认为你已经知道了:) – TonyMkenu
按照Tony的建议,我用[SSZipArchive unzipFileAtPath:zipPath toDestination:cachePath]替换了ZipArchive调用;但结果是一样的。 –