2014-01-21 45 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上的文件压缩器会创建一个额外的文件夹来放入压缩文件。 –

相关问题