Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 assign each image to a ui element

I'm working on a mac with cocoa and objective c

like image 785
Jamesp1989 Avatar asked Apr 19 '12 14:04

Jamesp1989


People also ask

How can I view a zip file without extracting it?

Vim command can also be used to view the contents of a ZIP archive without extracting it. It can work for both the archived files and folders. Along with ZIP, it can work with other extensions as well, such as tar. xz, tar.

What happens if you don't extract a zip file?

ZIP file without extracting them, they might not be copied or moved correctly, even though it looks like they have been.

Is it necessary to extract files from zip?

Because Windows makes it easy to work with zip files, there's generally no need to unzip them. However, if you would prefer to unzip them you can simply right-click the zip file's icon and select Extract All.


2 Answers

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];
like image 171
Jonas Schnelli Avatar answered Oct 04 '22 22:10

Jonas Schnelli


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:].

like image 33
Glen Low Avatar answered Oct 04 '22 20:10

Glen Low