How do I read image files directly from a zip without extracting to disk?

后端 未结 2 833
悲哀的现实
悲哀的现实 2020-12-11 05:47

What I want to do is assign images to ui elements at runtime (think Winamp style) but I have no idea how to go about reading from a zip without storing to disk. Or how to as

相关标签:
2条回答
  • 2020-12-11 06:22

    The zipzap version:

    ZZArchive* archive = [ZZArchive archiveWithContentsOfURL:[NSURL fileURLWithPath:@"test.zip"]];
    NSData* data = [archive.entries[0] newDataWithError:nil];
    

    Or if you're looking for a particular entry:

    ZZArchive* archive = [ZZArchive archiveWithContentsOfURL:[NSURL fileURLWithPath:@"test.zip"]];
    NSData* data = nil;
    for (ZZArchiveEntry* entry in archive.entries)
      if ([entry.fileName isEqualToString:@"test.txt"])
      {
        data = [entry newDataWithError:nil];
        break;
      }
    

    Scanning for files like this is pretty much optimal. The scan only uses the zip central directory and doesn't actually extract the data until -[entry newDataWithError:].

    0 讨论(0)
  • 2020-12-11 06:28

    Use objective-c zip (iOS/Mac zlib wrapper)

    Then you can do:

    ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeUnzip];
    [unzipFile goToFirstFileInZip];
    
    ZipReadStream *read= [unzipFile readCurrentFileInZip];
    NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
    int bytesRead= [read readDataWithBuffer:data];
    
    [read finishedReading];
    
    0 讨论(0)
提交回复
热议问题