我已經開始使用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調用;但結果是一樣的。 –