0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rick_CBR929RR
  • 197
  • 2
  • 15
  • Further tests have shown that even if I change my image and text files to the original names, and re-compress them with the original file name, the app still returns "no data". I'm beginning to think the problem is in the zipping, although I can't detect anything odd when unzipping on the Mac. – Rick_CBR929RR Jan 21 '14 at 08:07
  • P.S. the zip file from your http://. icodeblog .com/.../zipfile contains only 2 files... but I think you already know that :) – TonyMkenu Jan 23 '14 at 11:34
  • Following Tony's advice, I've replaced the ZipArchive call with the [SSZipArchive unzipFileAtPath:zipPath toDestination:cachePath]; but the result is the same. – Rick_CBR929RR Jan 23 '14 at 15:54
  • What a frustrating experience! The code works. But I didn't realize that the Mac compressor was creating an extra folder within the Zip. – Rick_CBR929RR Jan 23 '14 at 17:02

1 Answers1

0

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"
)
TonyMkenu
  • 7,597
  • 3
  • 27
  • 49
  • Thank you, Tony, for your efforts to help me. I'm still stuck, though. The directoryContents is helpful, but instead of the contents of the zip file it gives this - ZipTest DIR folder (contents of cache path): ( "__MACOSX", "com.brandontreb.ZipTest", zipfile, "zipfile.zip", "zipfile2.zip" ) When I run your demo project, and used the URL for the zipped file on my server, the directory contents are: testSSZipArchive DIR folder : ( "__MACOSX", "Archive.zip", "Tony-Mocanu.testSSZipArchive", zipfile ) But it won't find the desired contents. – Rick_CBR929RR Jan 23 '14 at 15:45
  • Try to test in a real device – TonyMkenu Jan 23 '14 at 16:29
  • Thank you, Tony! With your help I've discovered the (stupid) problem: the file compressor on my Mac creates a extra folder to put the zipped files into. – Rick_CBR929RR Jan 23 '14 at 17:00
  • Glad to help you Rick! :) – TonyMkenu Jan 23 '14 at 21:27