2014-01-21 26 views
0

我已經開始使用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。它運作良好。

+0

進一步的測試表明,即使我將圖像和文本文件更改爲原始名稱,並用原始文件名重新壓縮它們,應用程序仍會返回「無數據」。我開始認爲問題在於壓縮,儘管在Mac上解壓縮時我無法檢測到任何奇怪的問題。 –

+0

P.S.您的http://的zip文件。 icodeblog .com /.../ zipfile只包含2個文件...但我認爲你已經知道了:) – TonyMkenu

+0

按照Tony的建議,我用[SSZipArchive unzipFileAtPath:zipPath toDestination:cachePath]替換了ZipArchive調用;但結果是一樣的。 –

回答

0

我測試你的代碼...併爲我工作很好... 主要的變化: 代替:

ZipArchive *za = [[ZipArchive alloc] init]; 
       if ([za UnzipOpenFile: zipPath]) { 
        BOOL ret = [za UnzipFileTo: cachePath overWrite: YES]; 
        if (NO == ret){ 
        } 
        [za UnzipCloseFile]; 

我使用

[ SSZipArchive unzipFileAtPath:zipPath toDestination:cachePath];

你可以列出一個目錄的內容(解壓縮後):

NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:cachePath error:&error]; 
       NSLog(@"DIR folder : %@",directoryContents); 

您可以根據您的代碼https://dl.dropboxusercontent.com/u/19438780/testSSZipArchive.zip

UPDATE

測試在這裏查看我的演示項目我zip文件我有:

(
    "__MACOSX", 
    "Archive.zip", 
    "error feedly.txt", 
    "image1.jpg", 
    "image2.jpg", 
    "image3.jpg", 
    "image4.jpg", 
    "Tony-M.testSSZipArchive" 
) 
+0

謝謝你,託尼,爲你的努力來幫助我。不過,我仍然陷入困境。 directoryContents是有幫助的,但是它代替zip文件的內容 - ZipTest DIR文件夾(緩存路徑的內容):( 「__MACOSX」, 「com.brandontreb.ZipTest」, zipfile, 「zipfile。 zip「, 」zipfile2.zip「 )當我運行演示項目並使用我的服務器上壓縮文件的URL時,目錄內容爲:testSSZipArchive DIR文件夾:( 」__MACOSX「, 」Archive.zip 「, 」Tony-Mocanu.testSSZipArchive「, zipfile )但它不會找到所需的內容。 –

+0

嘗試在真實設備上測試 – TonyMkenu

+0

謝謝託尼!在你的幫助下,我發現了這個(愚蠢的)問題:我的Mac上的文件壓縮器會創建一個額外的文件夾來放入壓縮文件。 –

相關問題