I've started using ZipArchive because I have a testing app that displays images from a server where the content is often changing. I want the app to sort and display the images, without having an explicit list of the new image names. (Or maybe I should also include in the zipped file an XML document that contains the names of the new images.) Either way, I'm now having a problem adding a known image to the zip file on the server and finding it in the app.

The original zipfile.zip file has 2 files: photo.png and text.txt

That works. So I downloaded the zipfile.zip and uploaded it to my server. It still worked, of course. Then I unzipped it, added a new image to it, and re-zipped it on my server.

The updated zipfile.zip now has 3 files: photo.png, myphoto.png, and text.txt

The image I added, myphoto.png, can't be found. What am I missing?

- (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);
        }
    });
}

When I run this, looking for the image I added, it returns "no data". Why? And the bigger question is: Can I, in my iOS app, list the contents of an unzipped file?

I started this project with this question which mention this code: SSZipArchive. It works well.

有帮助吗?

解决方案

I've tested you code ... and is working fine for me... The main change: instead:

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

I'm using

[SSZipArchive unzipFileAtPath:zipPath toDestination:cachePath];

You can list the content of a directory (after unzip):

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

You can check here my demo project based on your code https://dl.dropboxusercontent.com/u/19438780/testSSZipArchive.zip

UPDATE

testing with my zip file I have:

(
    "__MACOSX",
    "Archive.zip",
    "error feedly.txt",
    "image1.jpg",
    "image2.jpg",
    "image3.jpg",
    "image4.jpg",
    "Tony-M.testSSZipArchive"
)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top